In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
import dateutil.parser
First, I made a mistake naming the data set! It's 2015 data, not 2014 data. But yes, still use
311-2014.csv. You can rename it.
Import your data, but only the first 200,000 rows. You'll also want to change the index to be a datetime based on the Created Date column - you'll want to check if it's already a datetime, and parse it if not.
In [3]:
df=pd.read_csv("311-2014.csv", nrows=200000)
In [4]:
dateutil.parser.parse(df['Created Date'][0])
Out[4]:
In [5]:
def parse_date(str_date):
return dateutil.parser.parse(str_date)
In [6]:
df['created_datetime']=df['Created Date'].apply(parse_date)
In [7]:
df.index=df['created_datetime']
What was the most popular type of complaint, and how many times was it filed?
In [8]:
df['Complaint Type'].describe()
Out[8]:
Make a horizontal bar graph of the top 5 most frequent complaint types.
In [9]:
df.groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5).plot(kind='barh').invert_yaxis()
Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
In [10]:
df.groupby(by='Borough')['Borough'].count()
Out[10]:
In [11]:
boro_pop={
'BRONX': 1438159,
'BROOKLYN': 2621793,
'MANHATTAN': 1636268,
'QUEENS': 2321580,
'STATEN ISLAND': 473279}
In [12]:
boro_df=pd.Series.to_frame(df.groupby(by='Borough')['Borough'].count())
boro_df['Population']=pd.DataFrame.from_dict(boro_pop, orient='index')
boro_df['Complaints']=boro_df['Borough']
boro_df.drop('Borough', axis=1, inplace=True)
boro_df['Per Capita']=boro_df['Complaints']/boro_df['Population']
boro_df['Per Capita'].plot(kind='bar')
Out[12]:
According to your selection of data, how many cases were filed in March? How about May?
In [13]:
df['2015-03']['Created Date'].count()
Out[13]:
In [14]:
df['2015-05']['Created Date'].count()
Out[14]:
I'd like to see all of the 311 complaints called in on April 1st.
Surprise! We couldn't do this in class, but it was just a limitation of our data set
In [15]:
df['2015-04-01']
Out[15]:
What was the most popular type of complaint on April 1st?
In [16]:
df['2015-04-01'].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(1)
Out[16]:
What were the most popular three types of complaint on April 1st
In [17]:
df['2015-04-01'].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(3)
Out[17]:
What month has the most reports filed? How many? Graph it.
In [18]:
df.resample('M')['Unique Key'].count().sort_values(ascending=False)
Out[18]:
In [19]:
df.resample('M').count().plot(y='Unique Key')
Out[19]:
What week of the year has the most reports filed? How many? Graph the weekly complaints.
In [20]:
df.resample('W')['Unique Key'].count().sort_values(ascending=False).head(5)
Out[20]:
In [21]:
df.resample('W').count().plot(y='Unique Key')
Out[21]:
Noise complaints are a big deal. Use .str.contains to select noise complaints, and make an chart of when they show up annually. Then make a chart about when they show up every day (cyclic).
In [32]:
noise_df=df[df['Complaint Type'].str.contains('Noise')]
noise_df.resample('M').count().plot(y='Unique Key')
Out[32]:
In [39]:
noise_df.groupby(by=noise_df.index.hour).count().plot(y='Unique Key')
Out[39]:
Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
In [42]:
df.resample('D')['Unique Key'].count().sort_values(ascending=False).head(5)
Out[42]:
In [47]:
df.resample('D')['Unique Key'].count().sort_values().tail(5).plot(kind='barh')
Out[47]:
What hour of the day are the most complaints? Graph a day of complaints.
In [74]:
df['Unique Key'].groupby(by=df.index.hour).count().sort_values(ascending=False)
Out[74]:
In [51]:
df['Unique Key'].groupby(df.index.hour).count().plot()
Out[51]:
One of the hours has an odd number of complaints. What are the most common complaints at that hour, and what are the most common complaints the hour before and after?
In [71]:
df[df.index.hour==0].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Out[71]:
In [72]:
df[df.index.hour==1].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Out[72]:
In [73]:
df[df.index.hour==11].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Out[73]:
So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
In [112]:
midnight_df = df[df.index.hour==0]
In [113]:
midnight_df.groupby(midnight_df.index.minute)['Unique Key'].count().sort_values(ascending=False)
Out[113]:
Looks like midnight is a little bit of an outlier. Why might that be? Take the 5 most common agencies and graph the times they file reports at (all day, not just midnight).
In [120]:
df.groupby('Agency')['Unique Key'].count().sort_values(ascending=False).head(5)
Out[120]:
In [139]:
ax=df[df['Agency']=='NYPD'].groupby(df[df['Agency']=='NYPD'].index.hour)['Unique Key'].count().plot(legend=True, label='NYPD')
df[df['Agency']=='HPD'].groupby(df[df['Agency']=='HPD'].index.hour)['Unique Key'].count().plot(ax=ax, legend=True, label='HPD')
df[df['Agency']=='DOT'].groupby(df[df['Agency']=='DOT'].index.hour)['Unique Key'].count().plot(ax=ax, legend=True, label='DOT')
df[df['Agency']=='DPR'].groupby(df[df['Agency']=='DPR'].index.hour)['Unique Key'].count().plot(ax=ax, legend=True, label='DPR')
df[df['Agency']=='DOHMH'].groupby(df[df['Agency']=='DOHMH'].index.hour)['Unique Key'].count().plot(ax=ax, legend=True, label='DOHMH')
Out[139]:
Graph those same agencies on an annual basis - make it weekly. When do people like to complain? When does the NYPD have an odd number of complaints?
In [141]:
ax=df[df['Agency']=='NYPD'].groupby(df[df['Agency']=='NYPD'].index.week)['Unique Key'].count().plot(legend=True, label='NYPD')
df[df['Agency']=='HPD'].groupby(df[df['Agency']=='HPD'].index.week)['Unique Key'].count().plot(ax=ax, legend=True, label='HPD')
df[df['Agency']=='DOT'].groupby(df[df['Agency']=='DOT'].index.week)['Unique Key'].count().plot(ax=ax, legend=True, label='DOT')
df[df['Agency']=='DPR'].groupby(df[df['Agency']=='DPR'].index.week)['Unique Key'].count().plot(ax=ax, legend=True, label='DPR')
df[df['Agency']=='DOHMH'].groupby(df[df['Agency']=='DOHMH'].index.week)['Unique Key'].count().plot(ax=ax, legend=True, label='DOHMH')
Out[141]:
Maybe the NYPD deals with different issues at different times? Check the most popular complaints in July and August vs the month of May. Also check the most common complaints for the Housing Preservation Bureau (HPD) in winter vs. summer.
In [142]:
nypd=df[df['Agency']=='NYPD']
In [157]:
nypd[(nypd.index.month==7) | (nypd.index.month==8)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Out[157]:
In [158]:
nypd[nypd.index.month==5].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Out[158]:
In [ ]:
# seems like mostly noise complaints and bad parking to me
In [159]:
hpd=df[df['Agency']=='HPD']
In [162]:
hpd[(hpd.index.month>=6) & (hpd.index.month<=8)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
# i would consider summer to be june to august.
Out[162]:
In [163]:
hpd[(hpd.index.month==12) | (hpd.index.month<=2)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Out[163]:
In [ ]:
# pretty similar list, but people probably notice a draft from their bad window or door in the winter more easily than summer