<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?
What were the most popular three types of complaint on April 1st
Step7:  What month has the most reports filed? How many? Graph it.
<ASSISTANT_TASK:>
Python Code:
df=pd.read_csv("311-2014.csv",nrows=20000)
df.head()
df.columns
df.info()
dateutil.parser.parse('07/16/1990').month
def parse_date (str_date):
    return dateutil.parser.parse(str_date)#dateutil is a module, import parser class, then transform a string into a python time object
df['Created Date']= df['Created Date'].apply(parse_date)
df.head(3)
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'].value_counts()
# the most popular type of complaint is blocked driveway, and it was filed 2534 times
Explanation: What was the most popular type of complaint, and how many times was it filed?
End of explanation
df['Complaint Type'].value_counts().head(5).sort_values().plot(kind='barh')
Explanation: Make a horizontal bar graph of the top 5 most frequent complaint types.
End of explanation
df['Borough'].value_counts()
people_bronx= 1438159
people_queens= 2321580
people_manhattan=1636268
people_brooklyn= 2621793
people_staten_island= 473279
complaints_per_capita_bronx= 29610/people_bronx
complaints_per_capita_bronx
Explanation: Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
End of explanation
[x for x in list(df['Created Date'].values)]# if np.datetime64(x, 'M') == 3]
date_index = pd.DatetimeIndex(df['Created Date'].values)#for dataframe, each column is a series(object), call a values method
len([x for x in date_index.month if x == 3])
iterable = filter(lambda x: x == 3, list(date_index.month))
march_days = 0
for x in iterable:
    march_days += 1
march_days
Explanation: According to your selection of data, how many cases were filed in March? How about May?
End of explanation
df.index=df['Created Date']
df.head()
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']
df['2015-04-01']['Complaint Type'].value_counts().head(3)
Explanation: What was the most popular type of complaint on April 1st?
What were the most popular three types of complaint on April 1st
End of explanation
df.resample('M').count().plot(y="Unique Key",legend=False)
#http://pandas.pydata.org/pandas-docs/stable/timeseries.html#up-and-downsampling
#resample is a time-based groupby, followed by a reduction method on each of its groups
ax= df.groupby(df.index.month).count().plot(y='Unique Key', legend=False)
ax.set_xticks([1,2,3,4,5,6,7,8,9,10,11, 12])
ax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
ax.set_ylabel("Number of Complaints")
ax.set_title("311 complains filed monthly in 2015")
#september has the most complaints cases filed
Explanation: What month has the most reports filed? How many? Graph it.
End of explanation
<END_TASK>