Module 9 · Data capstone: 911 calls · Lesson 42 of 66
Explore the calls
Where do emergency calls come from, and why? Start with counts and a new column.
In this lesson · about 25 minutes
- Answer questions about a new data set with value_counts and nunique
- Create a column by applying a function to text
- Turn text dates into real timestamps and pull out the hour, month and weekday
This capstone puts everything so far together: pandas, Seaborn and Matplotlib on a real public data set. Each row is one emergency (911) call in Montgomery County, Pennsylvania, between December 2015 and August 2016.
The columns:
lat,lng: where the call came fromdesc: a description of the emergencyzip: ZIP codetitle: the type of call, such asEMS: BACK PAINS/INJURYtimeStamp: when the call was loggedtwp: the townshipaddr: the addresse: always 1, handy for counting
The full file has about 99,000 calls. To keep the page fast we use a random sample of 20,000, so your counts will be about a fifth of those in other versions of this project. Each question loads the data as df. Make your last line the answer, then press Check.
Exercise
1. Top five ZIP codes
Which five ZIP codes make the most calls? Show the counts.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')Exercise
2. Top five townships
Now the five townships (twp) with the most calls.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')Exercise
3. How many kinds of call?
How many different title values are there?
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')A new column: the reason
Every title starts with a reason, a colon, and then details: EMS: BACK PAINS/INJURY, Fire: GAS-ODOR/LEAK, Traffic: VEHICLE ACCIDENT -. Splitting on the colon and keeping the first part gives the reason: EMS, Fire or Traffic.
Exercise
4. Make a Reason column
Create a column Reason that holds the part of title before the colon. Use .apply with a lambda.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')Exercise
5. The most common reason
Count the calls for each Reason.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')
df['Reason'] = df['title'].apply(lambda title: title.split(':')[0])Exercise
6. Chart the reasons
Draw a Seaborn count plot of calls by Reason.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')
df['Reason'] = df['title'].apply(lambda title: title.split(':')[0])Working with time
The timeStamp column is stored as text. pd.to_datetime turns it into real timestamps, and then the .dt accessor pulls out parts: .dt.hour, .dt.month, .dt.dayofweek (0 is Monday), .dt.date.
Exercise
7. Hour, month and weekday
Convert timeStamp to datetimes, then create three columns: Hour, Month and Day of Week. Make the weekday a name using this map:
dmap = {0: 'Mon', 1: 'Tue', 2: 'Wed', 3: 'Thu', 4: 'Fri', 5: 'Sat', 6: 'Sun'}
End with df[['Hour', 'Month', 'Day of Week']].
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('911.csv')
df['Reason'] = df['title'].apply(lambda title: title.split(':')[0])Exercises solved: 0 of 7
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.