Module 4 · pandas practice projects · Lesson 23 of 66
Project: San Francisco salaries
Answer real questions about city pay with pandas.
In this lesson · about 35 minutes
- Explore a new data set with head, info and describe
- Answer questions with filters, groupby and value_counts
- Write your own function and apply it to a column
Time to use pandas on real data. This file lists the pay of San Francisco city employees from 2011 to 2014, a public data set. Each row is one person in one year, with base pay, overtime, other pay, benefits and totals.
The full file has about 148,000 rows. To keep the page fast, we load a sample of 10,003 rows (2,500 per year, plus a few rows the questions need), so your answers will differ from versions of this exercise you may see elsewhere. That's expected.
Each question below already loads the data as sal. Make your last line the expression that answers the question, then press Check.
Take a first look
Always start a new data set by looking at it. head() shows the first rows, info() lists columns, types and missing values, and describe() summarizes the numbers.
Exercise
How many rows?
How many entries (rows) are in sal?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Average base pay
What is the average BasePay?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Most overtime
What is the highest OvertimePay in the data?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Joseph Driscoll's job
What is the job title of JOSEPH DRISCOLL? Use all capital letters: names in this file are uppercase.
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Joseph Driscoll's pay
How much does JOSEPH DRISCOLL make, including benefits (TotalPayBenefits)?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Highest paid
What is the name of the highest paid person, including benefits?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Lowest paid, and something strange
Show the full row of the lowest paid person, including benefits. Look closely at the pay. Anything strange?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Average base pay per year
What was the average BasePay for each year, 2011 to 2014?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
How many job titles?
How many unique job titles are there?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Five most common jobs
What are the 5 most common job titles, with their counts?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
One-of-a-kind jobs in 2013
How many job titles were held by only one person in 2013?
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
How many chiefs?
How many people have the word "chief" in their job title, in any capitalization? Write a function and use .apply().
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercise
Bonus: does a longer title pay more?
Add a column title_len with the length of each job title, then show the correlation between title_len and TotalPayBenefits with .corr(). A value near 0 means no relationship.
Code that runs before yours
import pandas as pd
sal = pd.read_csv('Salaries.csv')Exercises solved: 0 of 13
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.