<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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.
Importing and preparing your data
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.
Step1:  What was the most popular type of complaint, and how many times was it filed?
Step2:  Make a horizontal bar graph of the top 5 most frequent complaint types.
Step3:  Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
Step4:  According to your selection of data, how many cases were filed in March? How about May?
Step5:  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
Step6:  What was the most popular type of complaint on April 1st?
Step7:  What were the most popular three types of complaint on April 1st
Step8:  What month has the most reports filed? How many? Graph it.
Step9:  What week of the year has the most reports filed? How many? Graph the weekly complaints.
Step10:  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).
Step11:  Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
Step12:  What hour of the day are the most complaints? Graph a day of complaints.
Step13:  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?
Step14:  So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
Step15:  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).
Step16:  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?
Step17:  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.
<ASSISTANT_TASK:>
Python Code:
df=pd.read_csv("311-2014.csv", nrows=200000)
dateutil.parser.parse(df['Created Date'][0])
def parse_date(str_date):
    return dateutil.parser.parse(str_date)
df['created_datetime']=df['Created Date'].apply(parse_date)
df.index=df['created_datetime']
Explanation: 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.
Importing and preparing your data
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.
End of explanation
df['Complaint Type'].describe()
Explanation: What was the most popular type of complaint, and how many times was it filed?
End of explanation
df.groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5).plot(kind='barh').invert_yaxis()
Explanation: Make a horizontal bar graph of the top 5 most frequent complaint types.
End of explanation
df.groupby(by='Borough')['Borough'].count()
boro_pop={
    'BRONX': 1438159,
    'BROOKLYN': 2621793,
    'MANHATTAN': 1636268,
    'QUEENS': 2321580,
    'STATEN ISLAND': 473279}
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')
Explanation: Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
End of explanation
df['2015-03']['Created Date'].count()
df['2015-05']['Created Date'].count()
Explanation: According to your selection of data, how many cases were filed in March? How about May?
End of explanation
df['2015-04-01']
Explanation: 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
End of explanation
df['2015-04-01'].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(1)
Explanation: What was the most popular type of complaint on April 1st?
End of explanation
df['2015-04-01'].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(3)
Explanation: What were the most popular three types of complaint on April 1st
End of explanation
df.resample('M')['Unique Key'].count().sort_values(ascending=False)
df.resample('M').count().plot(y='Unique Key')
Explanation: What month has the most reports filed? How many? Graph it.
End of explanation
df.resample('W')['Unique Key'].count().sort_values(ascending=False).head(5)
df.resample('W').count().plot(y='Unique Key')
Explanation: What week of the year has the most reports filed? How many? Graph the weekly complaints.
End of explanation
noise_df=df[df['Complaint Type'].str.contains('Noise')]
noise_df.resample('M').count().plot(y='Unique Key')
noise_df.groupby(by=noise_df.index.hour).count().plot(y='Unique Key')
Explanation: 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).
End of explanation
df.resample('D')['Unique Key'].count().sort_values(ascending=False).head(5)
df.resample('D')['Unique Key'].count().sort_values().tail(5).plot(kind='barh')
Explanation: Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
End of explanation
df['Unique Key'].groupby(by=df.index.hour).count().sort_values(ascending=False)
df['Unique Key'].groupby(df.index.hour).count().plot()
Explanation: What hour of the day are the most complaints? Graph a day of complaints.
End of explanation
df[df.index.hour==0].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
df[df.index.hour==1].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
df[df.index.hour==11].groupby(by='Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
Explanation: 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?
End of explanation
midnight_df = df[df.index.hour==0]
midnight_df.groupby(midnight_df.index.minute)['Unique Key'].count().sort_values(ascending=False)
Explanation: So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
End of explanation
df.groupby('Agency')['Unique Key'].count().sort_values(ascending=False).head(5)
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')
Explanation: 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).
End of explanation
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')
Explanation: 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?
End of explanation
nypd=df[df['Agency']=='NYPD']
nypd[(nypd.index.month==7) | (nypd.index.month==8)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
nypd[nypd.index.month==5].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
# seems like mostly noise complaints and bad parking to me
hpd=df[df['Agency']=='HPD']
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.
hpd[(hpd.index.month==12) | (hpd.index.month<=2)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
# pretty similar list, but people probably notice a draft from their bad window or door in the winter more easily than summer
Explanation: 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.
End of explanation
<END_TASK>