In [1]:
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 [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
What was the most popular type of complaint, and how many times was it filed?
In [ ]:
df['Complaint Type'].value_counts().head(3)
Make a horizontal bar graph of the top 5 most frequent complaint types.
In [ ]:
df['Complaint Type'].value_counts().head(5)
In [ ]:
df['Complaint Type'].value_counts().head(5).sort_values(ascending=True).plot(kind='barh')
Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
In [ ]:
df['Borough'].value_counts()
In [ ]:
borough_counts=pd.DataFrame(df['Borough'].value_counts())
borough_counts['name']=borough_counts.index
borough_counts
In [ ]:
population_df['uppercase_name'] = population_df['name'].str.upper()
population_df
According to your selection of data, how many cases were filed in March? How about May?
In [ ]:
df['Created Date'].head(2)
In [ ]:
def parse_date(str_date):
return dateutil.parser.parse(str_date)
parse_date("07/06/2015 10:58:27 AM")
In [ ]:
#df['created_dt'] = df['Created Date'].apply(dateutil.parser.parse)
df['created_dt']=df['Created Date'].apply
In [ ]:
df.info()
In [ ]:
df.index
In [ ]:
len(df['2015-05'])
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 [ ]:
What was the most popular type of complaint on April 1st?
What were the most popular three types of complaint on April 1st
In [ ]:
april_first_df = df['2015-04-01']
april_first_df['Complaint Type'].value_counts().head(3)
In [ ]:
What month has the most reports filed? How many? Graph it.
In [ ]:
df.index.month[:15]
In [ ]:
df.groupby(by=df.index.month).count()
In [ ]:
df.resample('M')['Unique Key'].count()#resample: groupby both year and month because it treats 2015/05 different with 2014/05
In [ ]:
df.resample('M')['Unique Key'].count().plot()
What week of the year has the most reports filed? How many? Graph the weekly complaints.
In [ ]:
df.groupby(by=df.index.week)['Unique Key'].count().plot()
In [ ]:
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 [ ]:
noise_df.groupby(by=noise_df.index.hour)['Unique Key'].count().plot()#groupby only hour, only month
In [ ]:
Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
In [ ]:
df['Unique Key']
In [ ]:
What hour of the day are the most complaints? Graph a day of complaints.
In [ ]:
In [ ]:
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 [ ]:
In [ ]:
In [ ]:
So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
In [ ]:
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 [ ]:
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 [ ]:
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 [ ]:
df[df['Agency']=='NYPD']['2015-07']
In [ ]:
In [ ]:
In [ ]:
In [ ]: