more projects

This commit is contained in:
Austin
2026-02-03 09:00:30 -06:00
parent 2451448b8a
commit 4f40466733
250 changed files with 993861 additions and 0 deletions
+880
View File
@@ -0,0 +1,880 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SF Salaries Exercise \n",
"\n",
"A real-world Data set from Kaggle.com: [SF Salaries Dataset](https://www.kaggle.com/kaggle/sf-salaries)\n",
". Explore San Francisco city employee salary data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. Import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Read Salaries.csv as a dataframe called salary."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"salary = pd.read_csv(\"Salaries.csv\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Check the head and tail of the salary DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Id</th>\n",
" <th>EmployeeName</th>\n",
" <th>JobTitle</th>\n",
" <th>BasePay</th>\n",
" <th>OvertimePay</th>\n",
" <th>OtherPay</th>\n",
" <th>Benefits</th>\n",
" <th>TotalPay</th>\n",
" <th>TotalPayBenefits</th>\n",
" <th>Year</th>\n",
" <th>Notes</th>\n",
" <th>Agency</th>\n",
" <th>Status</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>NATHANIEL FORD</td>\n",
" <td>GENERAL MANAGER-METROPOLITAN TRANSIT AUTHORITY</td>\n",
" <td>167411.18</td>\n",
" <td>0.00</td>\n",
" <td>400184.25</td>\n",
" <td>NaN</td>\n",
" <td>567595.43</td>\n",
" <td>567595.43</td>\n",
" <td>2011</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>GARY JIMENEZ</td>\n",
" <td>CAPTAIN III (POLICE DEPARTMENT)</td>\n",
" <td>155966.02</td>\n",
" <td>245131.88</td>\n",
" <td>137811.38</td>\n",
" <td>NaN</td>\n",
" <td>538909.28</td>\n",
" <td>538909.28</td>\n",
" <td>2011</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>ALBERT PARDINI</td>\n",
" <td>CAPTAIN III (POLICE DEPARTMENT)</td>\n",
" <td>212739.13</td>\n",
" <td>106088.18</td>\n",
" <td>16452.60</td>\n",
" <td>NaN</td>\n",
" <td>335279.91</td>\n",
" <td>335279.91</td>\n",
" <td>2011</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>CHRISTOPHER CHONG</td>\n",
" <td>WIRE ROPE CABLE MAINTENANCE MECHANIC</td>\n",
" <td>77916.00</td>\n",
" <td>56120.71</td>\n",
" <td>198306.90</td>\n",
" <td>NaN</td>\n",
" <td>332343.61</td>\n",
" <td>332343.61</td>\n",
" <td>2011</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>PATRICK GARDNER</td>\n",
" <td>DEPUTY CHIEF OF DEPARTMENT,(FIRE DEPARTMENT)</td>\n",
" <td>134401.60</td>\n",
" <td>9737.00</td>\n",
" <td>182234.59</td>\n",
" <td>NaN</td>\n",
" <td>326373.19</td>\n",
" <td>326373.19</td>\n",
" <td>2011</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Id EmployeeName JobTitle \\\n",
"0 1 NATHANIEL FORD GENERAL MANAGER-METROPOLITAN TRANSIT AUTHORITY \n",
"1 2 GARY JIMENEZ CAPTAIN III (POLICE DEPARTMENT) \n",
"2 3 ALBERT PARDINI CAPTAIN III (POLICE DEPARTMENT) \n",
"3 4 CHRISTOPHER CHONG WIRE ROPE CABLE MAINTENANCE MECHANIC \n",
"4 5 PATRICK GARDNER DEPUTY CHIEF OF DEPARTMENT,(FIRE DEPARTMENT) \n",
"\n",
" BasePay OvertimePay OtherPay Benefits TotalPay TotalPayBenefits \\\n",
"0 167411.18 0.00 400184.25 NaN 567595.43 567595.43 \n",
"1 155966.02 245131.88 137811.38 NaN 538909.28 538909.28 \n",
"2 212739.13 106088.18 16452.60 NaN 335279.91 335279.91 \n",
"3 77916.00 56120.71 198306.90 NaN 332343.61 332343.61 \n",
"4 134401.60 9737.00 182234.59 NaN 326373.19 326373.19 \n",
"\n",
" Year Notes Agency Status \n",
"0 2011 NaN San Francisco NaN \n",
"1 2011 NaN San Francisco NaN \n",
"2 2011 NaN San Francisco NaN \n",
"3 2011 NaN San Francisco NaN \n",
"4 2011 NaN San Francisco NaN "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Id</th>\n",
" <th>EmployeeName</th>\n",
" <th>JobTitle</th>\n",
" <th>BasePay</th>\n",
" <th>OvertimePay</th>\n",
" <th>OtherPay</th>\n",
" <th>Benefits</th>\n",
" <th>TotalPay</th>\n",
" <th>TotalPayBenefits</th>\n",
" <th>Year</th>\n",
" <th>Notes</th>\n",
" <th>Agency</th>\n",
" <th>Status</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>148649</th>\n",
" <td>148650</td>\n",
" <td>Roy I Tillery</td>\n",
" <td>Custodian</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>2014</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148650</th>\n",
" <td>148651</td>\n",
" <td>Not provided</td>\n",
" <td>Not provided</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>2014</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148651</th>\n",
" <td>148652</td>\n",
" <td>Not provided</td>\n",
" <td>Not provided</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>2014</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148652</th>\n",
" <td>148653</td>\n",
" <td>Not provided</td>\n",
" <td>Not provided</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>2014</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148653</th>\n",
" <td>148654</td>\n",
" <td>Joe Lopez</td>\n",
" <td>Counselor, Log Cabin Ranch</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>-618.13</td>\n",
" <td>0.0</td>\n",
" <td>-618.13</td>\n",
" <td>-618.13</td>\n",
" <td>2014</td>\n",
" <td>NaN</td>\n",
" <td>San Francisco</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Id EmployeeName JobTitle BasePay \\\n",
"148649 148650 Roy I Tillery Custodian 0.0 \n",
"148650 148651 Not provided Not provided NaN \n",
"148651 148652 Not provided Not provided NaN \n",
"148652 148653 Not provided Not provided NaN \n",
"148653 148654 Joe Lopez Counselor, Log Cabin Ranch 0.0 \n",
"\n",
" OvertimePay OtherPay Benefits TotalPay TotalPayBenefits Year \\\n",
"148649 0.0 0.00 0.0 0.00 0.00 2014 \n",
"148650 NaN NaN NaN 0.00 0.00 2014 \n",
"148651 NaN NaN NaN 0.00 0.00 2014 \n",
"148652 NaN NaN NaN 0.00 0.00 2014 \n",
"148653 0.0 -618.13 0.0 -618.13 -618.13 2014 \n",
"\n",
" Notes Agency Status \n",
"148649 NaN San Francisco NaN \n",
"148650 NaN San Francisco NaN \n",
"148651 NaN San Francisco NaN \n",
"148652 NaN San Francisco NaN \n",
"148653 NaN San Francisco NaN "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.tail()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Check the .info() method to find out number of column, entries an dtype"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 148654 entries, 0 to 148653\n",
"Data columns (total 13 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 Id 148654 non-null int64 \n",
" 1 EmployeeName 148654 non-null object \n",
" 2 JobTitle 148654 non-null object \n",
" 3 BasePay 148045 non-null float64\n",
" 4 OvertimePay 148650 non-null float64\n",
" 5 OtherPay 148650 non-null float64\n",
" 6 Benefits 112491 non-null float64\n",
" 7 TotalPay 148654 non-null float64\n",
" 8 TotalPayBenefits 148654 non-null float64\n",
" 9 Year 148654 non-null int64 \n",
" 10 Notes 0 non-null float64\n",
" 11 Agency 148654 non-null object \n",
" 12 Status 0 non-null float64\n",
"dtypes: float64(8), int64(2), object(3)\n",
"memory usage: 14.7+ MB\n"
]
}
],
"source": [
"salary.info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 5. Find the average BasePay"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"66325.4488404877"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[\"BasePay\"].mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 6. Find the highest and lowest OvertimePay. "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"245131.88"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[\"OvertimePay\"].max()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.01"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[\"OvertimePay\"].min()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 7. Find number of missing values in BasePay"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"609"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"missing_base_pay = salary[\"BasePay\"].isna()\n",
"missing_base_pay.sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 8. Find the average (mean) BasePay in each year? (2011-2014)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Year\n",
"2011 63595.956517\n",
"2012 65436.406857\n",
"2013 69630.030216\n",
"2014 66564.421924\n",
"Name: BasePay, dtype: float64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.groupby(\"Year\")[\"BasePay\"].mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 9. Find number of unique job titles are there "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2159"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[\"JobTitle\"].nunique()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 10. Find the top 10 popular jobs by JobTitle"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"JobTitle\n",
"Transit Operator 7036\n",
"Special Nurse 4389\n",
"Registered Nurse 3736\n",
"Public Svc Aide-Public Works 2518\n",
"Police Officer 3 2421\n",
"Custodian 2418\n",
"TRANSIT OPERATOR 2388\n",
"Firefighter 2359\n",
"Recreation Leader 1971\n",
"Patient Care Assistant 1945\n",
"Name: count, dtype: int64"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[\"JobTitle\"].value_counts().head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 12. Find the job title of KEVIN TAYLOR."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'BATTALION CHIEF, (FIRE DEPARTMENT)'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[salary[\"EmployeeName\"] == \"KEVIN TAYLOR\"][\"JobTitle\"].values[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 13. Find how much ROBERT TAI makes (TotalPayBenefits)."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"206361.14"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[salary[\"EmployeeName\"] == \"ROBERT TAI\"][\"TotalPayBenefits\"].values[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 14. Find how many JobTitles were only hold by 2 people in the year of 2012. "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"90"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(salary[salary[\"Year\"] == 2012][\"JobTitle\"].value_counts() == 2).sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 15. Find the name of the employee who makes the maximum salary (TotalPayBenefits) and find that amount "
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'NATHANIEL FORD'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.loc[salary[\"TotalPayBenefits\"].idxmax(), \"EmployeeName\"]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"567595.43"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.loc[salary[\"TotalPayBenefits\"].idxmax(), \"TotalPayBenefits\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 16. Find the name of the employee who makes the minimum salary (TotalPayBenefits) and find that amount "
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Joe Lopez'"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.loc[salary[\"TotalPayBenefits\"].idxmin(), \"EmployeeName\"]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-618.13"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary.loc[salary[\"TotalPayBenefits\"].idxmin(), \"TotalPayBenefits\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 16. Find the number of people who has CAPTAIN word in their job title"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"552"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[salary[\"JobTitle\"].str.contains(\"CAPTAIN\", case=False)].shape[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 17. Find the correlation between the length of the JobTitle string and TotalPayBenefits"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.03687844593260671"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"salary[\"JobTitle\"].apply(len).corr(salary[\"TotalPayBenefits\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"negative correlation "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Good Job!\n",
"next up matplotlib"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long