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 [2]:
df = pd.read_csv("311-2015.csv", nrows=200000, low_memory=False)
#Why can't we use parse_dates=True? --> If the date is in a format that pandas recognizes as a date like YYYYMMDD
#Keep the data set small and make sure everything works before running everything through it
In [3]:
df.head()
Out[3]:
In [4]:
df.info()
In [8]:
def parse_date(str_date):
return dateutil.parser.parse(str_date)
df['created_datetime'] = df['Created Date'].apply(parse_date)
df.head()
Out[8]:
In [9]:
df.index = df['created_datetime']
In [10]:
df.head()
Out[10]:
In [ ]:
What was the most popular type of complaint, and how many times was it filed?
In [3]:
df['Complaint Type'].value_counts().head(1)
Out[3]:
Make a horizontal bar graph of the top 5 most frequent complaint types.
In [6]:
df['Complaint Type'].value_counts().head().sort_values().plot(kind='barh')
Out[6]:
Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
In [11]:
df['Borough'].value_counts()
Out[11]:
In [12]:
brooklyn_population = 2636735
manhattan_population = 1644518
queens_population = 2339150
bronx_population = 1455444
staten_island_population = 474558
#http://www1.nyc.gov/site/planning/data-maps/nyc-population/current-future-populations.page
In [13]:
brooklyn_complaints_pc = 57129 / brooklyn_population
manhattan_complaints_pc = 42050 / manhattan_population
bronx_complaints_pc = 29610 / bronx_population
queens_complaints_pc = 46824 / queens_population
staten_island_complaints_pc = 7387 / staten_island_population
print(brooklyn_complaints_pc, manhattan_complaints_pc, bronx_complaints_pc, queens_complaints_pc, staten_island_complaints_pc)
print("Manhattan whines the most")
In [7]:
#Here's how to convert the data into a dataframe
borough_counts = pd.DataFrame(df['Borough'].value_counts())
borough_counts['name'] = borough_counts.index
borough_counts
Out[7]:
According to your selection of data, how many cases were filed in March? How about May?
In [11]:
df['2015-03']['Unique Key'].count()
Out[11]:
In [12]:
df['2015-05']['Unique Key'].count()
Out[12]:
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 [13]:
df['2015-04-01']['Unique Key'].count()
Out[13]:
What was the most popular type of complaint on April 1st?
What were the most popular three types of complaint on April 1st
In [14]:
df['2015-04-01']['Complaint Type'].value_counts().head(3)
Out[14]:
In [ ]:
What month has the most reports filed? How many? Graph it.
In [18]:
ax = df.resample('M')['Unique Key'].count().plot(kind='barh')
ax.set_yticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
ax.set_ylabel("Month")
Out[18]:
What week of the year has the most reports filed? How many? Graph the weekly complaints.
In [ ]:
df.index.week
In [17]:
ax = df.groupby(by=df.index.week)['Unique Key'].count().plot()
ax.set_xlabel("Week")
Out[17]:
In [19]:
df.resample('W')['Unique Key'].count().plot()
Out[19]:
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 [20]:
is_noise_complaints = df['Complaint Type'].str.contains("Noise")
ax = df[is_noise_complaints].resample('M').count().plot(y='Unique Key')
ax = df[is_noise_complaints].resample('D').count().plot(y='Unique Key')
In [21]:
#Using a noise df group by the hour
#noise_df.groupby(by=noise_df.index.hour)['Unique Key'].count.plot()
Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
In [22]:
ax = df['created_datetime'].value_counts().head().plot(kind='barh')
ax.set_ylabel("Top 5 Days For Complaints")
Out[22]:
What hour of the day are the most complaints? Graph a day of complaints.
In [ ]:
#df['created_datetime'].resample('H').count()
df.groupby(by=df.index.hour).count().plot(y='Unique Key')
Out[ ]:
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 [25]:
#df.sort_values(by=df.index.hour).head()
midnight_df = df[df.index.hour == 0]
midnight_df.head()
Out[25]:
In [28]:
midnight_df['Complaint Type'].value_counts().head(3)
Out[28]:
In [29]:
df[df.index.hour == 23]['Complaint Type'].value_counts().head(3)
Out[29]:
In [30]:
df[df.index.hour == 1]['Complaint Type'].value_counts().head(3)
Out[30]:
So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
In [31]:
#Wrong becuase we don't want every minute of every day
#midnight_df.resample('T')['Unique Key'].count().plot()
midnight_df.groupby(by=midnight_df.index.minute)['Unique Key'].count().plot()
Out[31]:
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 [36]:
real_midnight = df[(df.index.hour == 0) & (df.index.minute == 0) & (df.index.second == 0)]
len(real_midnight)
Out[36]:
In [39]:
twelve_and_one_sec = df[(df.index.hour == 0) & (df.index.minute == 0) & (df.index.second == 1)]
len(twelve_and_one_sec)
Out[39]:
In [40]:
twelve_o_one = df[(df.index.hour == 0) & (df.index.minute == 2) & (df.index.second == 0)]
len(twelve_o_one)
Out[40]:
In [41]:
real_midnight['Agency'].value_counts()
Out[41]:
In [43]:
df['Agency'].value_counts().head()
Out[43]:
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 [45]:
df[df['Agency'] == 'NYPD']['2015-7':'2015-8']['Complaint Type'].value_counts().head()
Out[45]:
In [46]:
df[df['Agency'] == 'NYPD']['2015-5']['Complaint Type'].value_counts().head()
Out[46]:
In [47]:
df[df['Agency'] == 'HPD']['2015-01':'2015-02']['Complaint Type'].value_counts().head()
Out[47]:
In [48]:
df[df['Agency'] == 'HPD']['2015-05':'2015-08']['Complaint Type'].value_counts().head()
Out[48]:
In [ ]: