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)
In [3]:
#total number of rows
print("Total rows: {0}".format(len(df)))
In [4]:
#Alternative way to do it
#df2 = df.ix[:200000]
In [6]:
print("Total rows: {0}".format(len(df))) #20000 data points+1 for columns name
In [7]:
#you can see that df2 dataframe displays 20000 rows of data.
df.tail()
Out[7]:
In [ ]:
#time variables like "Create Date" and "Closed Date" are not considered datatime
df.info()
df.columns
In [117]:
def parse_date(str_date):
return dateutil.parser.parse(str_date)
parse_date("07/06/2015 10:58:27 AM")
Out[117]:
In [115]:
df['Created Date'].head(2)
Out[115]:
In [118]:
df['Date'] = df['Created Date'].apply(parse_date)
df.head(2)
Out[118]:
In [10]:
#print(df['Date'])
df.info()
In [11]:
df.index = df['Date']
In [12]:
del df['Date']
In [13]:
df.head(10)
Out[13]:
What was the most popular type of complaint, and how many times was it filed?
In [14]:
df['Complaint Type'].describe()
#top= mode. Frequency: 21779 cases
Out[14]:
In [15]:
#using mode function in complain type
df['Complaint Type'].mode()
Out[15]:
Make a horizontal bar graph of the top 5 most frequent complaint types.
In [18]:
complains = df.groupby('Complaint Type').count()
In [20]:
complains.sort('Unique Key', ascending=False).head(5)
Out[20]:
In [21]:
COM = complains.sort('Unique Key', ascending=False)
In [30]:
COM['Unique Key'].head(5)
Out[30]:
In [109]:
COM['Unique Key'].head(5).sort_values().plot.barh()
Out[109]:
In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
COMG.plot.hist()
Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
In [61]:
#Now NaNs are 0
df["Incident Zip"].fillna(0, inplace=True)
Out[61]:
In [92]:
complains = df['Borough'].value_counts()
Out[92]:
In [112]:
#brooklyn 2592000
#staten island 472621
# 1419000
#Man 1626000
#queens 2296000
bcount = pd.DataFrame(df['Borough'].value_counts())
bcount['name'] = bcount.index
bcount
Out[112]:
In [113]:
#Merged in pandas need to be learn to solve this!
According to your selection of data, how many cases were filed in March? How about May?
In [97]:
March = df['2015-03']['Unique Key'].count()
May = df['2015-05']['Unique Key'].count()
print("March:", March)
print("May:", May)
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 [98]:
df['2015-04-01']['Unique Key'].count()
Out[98]:
What was the most popular type of complaint on April 1st?
What were the most popular three types of complaint on April 1st
In [108]:
top1 = df['2015-04-01']['Complaint Type'].value_counts().head(1)
top3 = df['2015-04-01']['Complaint Type'].value_counts().head(3)
print("April#1:", top1)
print("----------------------")
print("April TOP3:", top3)
What month has the most reports filed? How many? Graph it.
In [119]:
df.index.month[:15]
Out[119]:
In [121]:
df.groupby(by=df.index.month)['Unique Key'].count()
Out[121]:
In [122]:
df.groupby(by=df.index.month)['Unique Key'].count().plot() #all months no matter what year
Out[122]:
In [123]:
df.resample('M')['Unique Key'].count() #resample takes care of the month of this year.
Out[123]:
In [124]:
df.resample('M')['Unique Key'].count().plot()
Out[124]:
What week of the year has the most reports filed? How many? Graph the weekly complaints.
In [125]:
df.index.week
Out[125]:
In [126]:
df.groupby(by=df.index.week)['Unique Key'].count().plot()
Out[126]:
In [127]:
df.resample('W')['Unique Key'].count().plot()
Out[127]:
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 [130]:
noise = df['Complaint Type'].str.contains("Noise")
noise_df = df[noise]
noise_df.head(2)
Out[130]:
In [133]:
noise_df.resample('W')['Unique Key'].count().plot()
Out[133]:
In [134]:
noise_df.resample('H')['Unique Key'].count().plot() #resample emember what day, month, hour and year. Waht we want is
#by day only.
Out[134]:
In [135]:
noise_df.groupby(by=noise_df.index.hour)['Unique Key'].count().plot()
Out[135]:
Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
In [140]:
df['Unique Key'].resample('D').count().sort_values(ascending=True).head(5).plot.barh()
Out[140]:
In [ ]:
What hour of the day are the most complaints? Graph a day of complaints.
In [ ]:
df.groupby()
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 [152]:
midnigth_df = df[df.index.hour == 0]
In [155]:
midnigth_df.head(2)
Out[155]:
In [ ]:
So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
In [ ]:
midnigth_df.groupby(by=midnigth_df.index.minute)['Unique Key'].
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 [157]:
df[df['Agency'] == 'NYPD']['2015-07':'2015-08']['Complaint Type'].value_counts().head(5)
Out[157]:
In [159]:
df[df['Agency'] == 'NYPD']['2015-06':'2015-08']['Complaint Type'].value_counts().head(5)
Out[159]:
In [158]:
df[df['Agency'] == 'HPD']['2015-07':'2015-08']['Complaint Type'].value_counts().head(5)
Out[158]:
In [162]:
df[df['Agency'] == 'HPD']['2015-01':'2015-02']['Complaint Type'].value_counts().head(5)
Out[162]:
In [164]:
df[df['Agency'] == 'HPD']['2015-05':'2015-08']['Complaint Type'].value_counts()
Out[164]:
In [ ]: