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.

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.


In [2]:
#df = pd.read_csv('311-2010-2016.csv')
# We select a list of columns for a better efficiency
columns_list = ['Unique Key', 'Created Date', 'Closed Date', 'Agency', 'Agency Name',
       'Complaint Type', 'Descriptor', 'Borough']
df = pd.read_csv('311-2015.csv', nrows=200000, usecols= columns_list)

In [3]:
df['Created Date'].describe()


Out[3]:
count                     200000
unique                    177667
top       05/04/2015 12:00:00 AM
freq                        1034
Name: Created Date, dtype: object

In [4]:
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']
df.head(2)


Out[4]:
Unique Key Created Date Closed Date Agency Agency Name Complaint Type Descriptor Borough created_datetime
created_datetime
2015-07-06 10:58:27 31015465 07/06/2015 10:58:27 AM 07/22/2015 01:07:20 AM DCA Department of Consumer Affairs Consumer Complaint Demand for Cash QUEENS 2015-07-06 10:58:27
2015-07-03 13:26:29 30997660 07/03/2015 01:26:29 PM 07/03/2015 02:08:20 PM NYPD New York City Police Department Vending In Prohibited Area MANHATTAN 2015-07-03 13:26:29

In [ ]:

What was the most popular type of complaint, and how many times was it filed?


In [7]:
top_five_complaints = []
complaints_count = df['Complaint Type'].value_counts().head(5)
for complaint_type, count in complaints_count.items():
    top_five_complaints.append({"type": complaint_type, "count": count})

print("The most popular type of complaint is", top_five_complaints[0]['type'], "and it was filed", top_five_complaints[0]['count'], "times.")


The most popular type of complaint is Blocked Driveway and it was filed 21779 times.

Make a horizontal bar graph of the top 5 most frequent complaint types.


In [8]:
top5_df = pd.DataFrame(top_five_complaints)

top5_df.plot(kind='barh', x='type', y='count', label='Top 5 most frequent complaint types').invert_yaxis()
fig = plt.gcf()
fig.set_size_inches(18, 7, forward=True)
plt.xlabel("Count")
plt.ylabel("Type")


Out[8]:
<matplotlib.text.Text at 0x10e226e48>

Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.


In [9]:
boroughs_dict_count = df.groupby(by='Borough')['Unique Key'].agg(['count']).to_dict()['count']
boroughs_dict_pop = {'BRONX': 1438159,
 'BROOKLYN': 2621793,
 'MANHATTAN': 1636268,
 'QUEENS': 2321580,
 'STATEN ISLAND': 47327}
complaints_per_capita = {}

for borough, count in boroughs_dict_count.items():
    if borough != 'Unspecified':
        complaints_per_capita[borough] = count/boroughs_dict_pop[borough]
#        print(borough.title(), "has", count/boroughs_dict_pop[borough], "complaints per capita.")

most_complaints_per_capita = max(complaints_per_capita, key=lambda i: complaints_per_capita[i])

answer = "{} has the most complaints per capita, with {:.3f} complaints per capita.".format(most_complaints_per_capita.title(), complaints_per_capita[most_complaints_per_capita])
print(answer)


Staten Island has the most complaints per capita, with 0.156 complaints per capita.

According to your selection of data, how many cases were filed in March? How about May?


In [10]:
print(len(df['2015-03']), "cases were filed in March.")
print(len(df['2015-05']), "cases were filed in May.")


15025 cases were filed in March.
49715 cases were filed in 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 [11]:
april_1 = df['2015-04-01']
april_1


Out[11]:
Unique Key Created Date Closed Date Agency Agency Name Complaint Type Descriptor Borough created_datetime
created_datetime
2015-04-01 21:37:42 30311691 04/01/2015 09:37:42 PM 04/01/2015 10:49:33 PM NYPD New York City Police Department Illegal Parking Blocked Sidewalk BROOKLYN 2015-04-01 21:37:42
2015-04-01 23:12:04 30307701 04/01/2015 11:12:04 PM 04/01/2015 11:32:40 PM NYPD New York City Police Department Noise - Commercial Loud Music/Party BROOKLYN 2015-04-01 23:12:04
2015-04-01 13:10:35 30313389 04/01/2015 01:10:35 PM 04/07/2015 04:01:08 PM DPR Department of Parks and Recreation Root/Sewer/Sidewalk Condition Trees and Sidewalks Program QUEENS 2015-04-01 13:10:35
2015-04-01 17:37:38 30314393 04/01/2015 05:37:38 PM 04/03/2015 11:40:54 AM DPR Department of Parks and Recreation Maintenance or Facility Hours of Operation BROOKLYN 2015-04-01 17:37:38
2015-04-01 12:32:40 30309207 04/01/2015 12:32:40 PM 04/17/2015 01:06:49 AM DCA Department of Consumer Affairs Consumer Complaint Installation/Work Quality QUEENS 2015-04-01 12:32:40
2015-04-01 18:44:50 30311759 04/01/2015 06:44:50 PM 06/24/2015 11:27:00 AM DPR Department of Parks and Recreation Damaged Tree Entire Tree Has Fallen Down BRONX 2015-04-01 18:44:50
2015-04-01 16:30:15 30309690 04/01/2015 04:30:15 PM 04/01/2015 11:27:22 PM NYPD New York City Police Department Animal Abuse Neglected QUEENS 2015-04-01 16:30:15
2015-04-01 09:04:07 30307990 04/01/2015 09:04:07 AM 04/06/2015 09:17:10 AM DOF Senior Citizen Rent Increase Exemption Unit SCRIE Miscellaneous MANHATTAN 2015-04-01 09:04:07
2015-04-01 07:46:58 30308253 04/01/2015 07:46:58 AM 04/01/2015 09:32:31 AM NYPD New York City Police Department Blocked Driveway No Access QUEENS 2015-04-01 07:46:58
2015-04-01 17:12:17 30314214 04/01/2015 05:12:17 PM 04/09/2015 02:20:11 PM DOT Department of Transportation Highway Condition Pothole - Highway QUEENS 2015-04-01 17:12:17
2015-04-01 21:30:48 30307111 04/01/2015 09:30:48 PM NaN DOHMH Department of Health and Mental Hygiene Food Establishment Food Temperature BROOKLYN 2015-04-01 21:30:48
2015-04-01 15:51:04 30311571 04/01/2015 03:51:04 PM 04/14/2015 09:23:30 AM DPR Department of Parks and Recreation Maintenance or Facility Hours of Operation BROOKLYN 2015-04-01 15:51:04
2015-04-01 10:43:28 30313817 04/01/2015 10:43:28 AM NaN DPR Department of Parks and Recreation Damaged Tree Branch Cracked and Will Fall MANHATTAN 2015-04-01 10:43:28
2015-04-01 15:12:46 30308922 04/01/2015 03:12:46 PM 06/01/2015 06:25:48 AM DOHMH Department of Health and Mental Hygiene Food Establishment Letter Grading BROOKLYN 2015-04-01 15:12:46
2015-04-01 06:15:42 30311132 04/01/2015 06:15:42 AM 04/01/2015 10:28:30 AM DOT Department of Transportation Highway Condition Pothole - Highway STATEN ISLAND 2015-04-01 06:15:42
2015-04-01 11:28:02 30308180 04/01/2015 11:28:02 AM 04/01/2015 11:42:53 AM DOT Department of Transportation Highway Condition Pothole - Highway QUEENS 2015-04-01 11:28:02
2015-04-01 17:35:18 30313207 04/01/2015 05:35:18 PM 06/01/2015 06:25:54 AM DOHMH Department of Health and Mental Hygiene Food Establishment Rodents/Insects/Garbage MANHATTAN 2015-04-01 17:35:18
2015-04-01 13:54:54 30310017 04/01/2015 01:54:54 PM 04/06/2015 10:11:11 AM DOF Senior Citizen Rent Increase Exemption Unit SCRIE Miscellaneous QUEENS 2015-04-01 13:54:54
2015-04-01 23:49:33 30306774 04/01/2015 11:49:33 PM 04/02/2015 12:20:59 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party MANHATTAN 2015-04-01 23:49:33
2015-04-01 07:50:49 30313339 04/01/2015 07:50:49 AM 07/08/2015 02:19:25 PM DOT Department of Transportation Street Condition Rough, Pitted or Cracked Roads QUEENS 2015-04-01 07:50:49
2015-04-01 13:50:29 30312146 04/01/2015 01:50:29 PM 06/01/2015 06:25:49 AM DOHMH Department of Health and Mental Hygiene Food Establishment Rodents/Insects/Garbage MANHATTAN 2015-04-01 13:50:29
2015-04-01 16:14:19 30313259 04/01/2015 04:14:19 PM 04/01/2015 04:21:53 PM HRA HRA Benefit Card Replacement Benefit Card Replacement Medicaid Unspecified 2015-04-01 16:14:19
2015-04-01 19:27:34 30308920 04/01/2015 07:27:34 PM 04/01/2015 08:45:17 PM NYPD New York City Police Department Noise - Street/Sidewalk Loud Music/Party MANHATTAN 2015-04-01 19:27:34
2015-04-01 05:30:02 30314164 04/01/2015 05:30:02 AM 04/01/2015 02:57:31 PM DOT Department of Transportation Highway Condition Pothole - Highway BROOKLYN 2015-04-01 05:30:02
2015-04-01 10:33:26 30311790 04/01/2015 10:33:26 AM 04/01/2015 11:19:12 AM NYPD New York City Police Department Illegal Parking Blocked Sidewalk MANHATTAN 2015-04-01 10:33:26
2015-04-01 11:47:38 30310940 04/01/2015 11:47:38 AM 04/06/2015 09:23:32 AM DOF Senior Citizen Rent Increase Exemption Unit SCRIE Miscellaneous QUEENS 2015-04-01 11:47:38
2015-04-01 11:01:27 30310409 04/01/2015 11:01:27 AM 04/17/2015 01:06:42 AM DCA Department of Consumer Affairs Consumer Complaint Exchange/Refund/Return BRONX 2015-04-01 11:01:27
2015-04-01 08:51:52 30310350 04/01/2015 08:51:52 AM 04/03/2015 04:33:46 PM DCA Department of Consumer Affairs Consumer Complaint Cars Parked on Sidewalk/Street BROOKLYN 2015-04-01 08:51:52
2015-04-01 14:58:55 30313106 04/01/2015 02:58:55 PM 04/06/2015 10:06:35 AM DOF Senior Citizen Rent Increase Exemption Unit SCRIE Rent Discrepancy BROOKLYN 2015-04-01 14:58:55
2015-04-01 16:59:19 30309324 04/01/2015 04:59:19 PM 04/01/2015 07:48:33 PM NYPD New York City Police Department Blocked Driveway Partial Access BROOKLYN 2015-04-01 16:59:19
... ... ... ... ... ... ... ... ... ...
2015-04-01 17:12:09 30313532 04/01/2015 05:12:09 PM 04/30/2015 06:02:47 PM DOT Department of Transportation Street Condition Line/Marking - Faded BROOKLYN 2015-04-01 17:12:09
2015-04-01 17:09:29 30311473 04/01/2015 05:09:29 PM 04/30/2015 05:59:38 PM DOT Department of Transportation Street Condition Line/Marking - Faded BROOKLYN 2015-04-01 17:09:29
2015-04-01 18:30:22 30307427 04/01/2015 06:30:22 PM 05/06/2015 10:59:47 AM DOT Department of Transportation Street Condition Failed Street Repair BROOKLYN 2015-04-01 18:30:22
2015-04-01 21:07:21 30314301 04/01/2015 09:07:21 PM 05/08/2015 11:30:22 AM TLC Taxi and Limousine Commission Taxi Complaint Driver Complaint MANHATTAN 2015-04-01 21:07:21
2015-04-01 10:50:12 30312508 04/01/2015 10:50:12 AM 05/08/2015 10:21:38 AM TLC Taxi and Limousine Commission Taxi Complaint Driver Complaint MANHATTAN 2015-04-01 10:50:12
2015-04-01 09:07:38 30310225 04/01/2015 09:07:38 AM 05/04/2015 10:43:15 AM DPR Department of Parks and Recreation Root/Sewer/Sidewalk Condition Trees and Sidewalks Program STATEN ISLAND 2015-04-01 09:07:38
2015-04-01 16:18:25 30313554 04/01/2015 04:18:25 PM 05/08/2015 11:29:12 AM TLC Taxi and Limousine Commission Taxi Complaint Driver Complaint QUEENS 2015-04-01 16:18:25
2015-04-01 10:23:09 30313061 04/01/2015 10:23:09 AM 05/07/2015 02:19:57 PM TLC Taxi and Limousine Commission Taxi Complaint Driver Complaint MANHATTAN 2015-04-01 10:23:09
2015-04-01 14:31:57 30312110 04/01/2015 02:31:57 PM 05/08/2015 06:05:44 PM DPR Department of Parks and Recreation Dead Tree Dead/Dying Tree BROOKLYN 2015-04-01 14:31:57
2015-04-01 18:50:19 30307758 04/01/2015 06:50:19 PM 05/07/2015 07:46:48 AM DPR Department of Parks and Recreation Damaged Tree Branch Cracked and Will Fall QUEENS 2015-04-01 18:50:19
2015-04-01 14:03:43 30313462 04/01/2015 02:03:43 PM 05/06/2015 12:48:52 PM DOT Department of Transportation Street Condition Blocked - Construction BROOKLYN 2015-04-01 14:03:43
2015-04-01 11:59:20 30310246 04/01/2015 11:59:20 AM 11/09/2015 03:58:34 PM DOT Department of Transportation Street Condition Rough, Pitted or Cracked Roads BROOKLYN 2015-04-01 11:59:20
2015-04-01 09:17:40 30310085 04/01/2015 09:17:40 AM 05/07/2015 06:53:11 PM DOT Department of Transportation Highway Condition Graffiti - Highway BROOKLYN 2015-04-01 09:17:40
2015-04-01 21:13:08 30314474 04/01/2015 09:13:08 PM 05/08/2015 11:27:01 AM TLC Taxi and Limousine Commission Taxi Complaint Driver Complaint QUEENS 2015-04-01 21:13:08
2015-04-01 12:59:08 30308968 04/01/2015 12:59:08 PM 04/01/2015 12:59:23 PM HRA HRA Benefit Card Replacement Benefit Card Replacement Medicaid Unspecified 2015-04-01 12:59:08
2015-04-01 13:31:23 30308389 04/01/2015 01:31:23 PM 04/01/2015 01:32:08 PM HRA HRA Benefit Card Replacement Benefit Card Replacement Medicaid Unspecified 2015-04-01 13:31:23
2015-04-01 16:42:15 30309377 04/01/2015 04:42:15 PM 04/01/2015 04:43:11 PM HRA HRA Benefit Card Replacement Benefit Card Replacement Food Stamp Unspecified 2015-04-01 16:42:15
2015-04-01 13:37:07 30310992 04/01/2015 01:37:07 PM 04/01/2015 01:37:28 PM HRA HRA Benefit Card Replacement Benefit Card Replacement Food Stamp Unspecified 2015-04-01 13:37:07
2015-04-01 23:44:04 30310652 04/01/2015 11:44:04 PM 04/02/2015 01:25:52 AM NYPD New York City Police Department Derelict Vehicle With License Plate QUEENS 2015-04-01 23:44:04
2015-04-01 16:32:12 30309028 04/01/2015 04:32:12 PM 05/20/2015 05:36:29 PM TLC Taxi and Limousine Commission For Hire Vehicle Complaint Car Service Company Complaint BRONX 2015-04-01 16:32:12
2015-04-01 08:26:06 30312622 04/01/2015 08:26:06 AM 06/01/2015 06:25:41 AM DOHMH Department of Health and Mental Hygiene Food Establishment Facility Construction BROOKLYN 2015-04-01 08:26:06
2015-04-01 15:08:20 30308371 04/01/2015 03:08:20 PM 06/01/2015 06:18:02 PM TLC Taxi and Limousine Commission Taxi Complaint Driver Complaint MANHATTAN 2015-04-01 15:08:20
2015-04-01 10:19:21 30311001 04/01/2015 10:19:21 AM 06/01/2015 06:25:39 AM DOHMH Department of Health and Mental Hygiene Food Establishment Rodents/Insects/Garbage QUEENS 2015-04-01 10:19:21
2015-04-01 20:20:13 30311341 04/01/2015 08:20:13 PM 04/01/2015 10:49:32 PM NYPD New York City Police Department Blocked Driveway Partial Access QUEENS 2015-04-01 20:20:13
2015-04-01 02:16:44 30308863 04/01/2015 02:16:44 AM 04/01/2015 02:54:17 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party MANHATTAN 2015-04-01 02:16:44
2015-04-01 13:12:58 30307673 04/01/2015 01:12:58 PM 04/01/2015 10:01:26 PM NYPD New York City Police Department Illegal Parking Posted Parking Sign Violation STATEN ISLAND 2015-04-01 13:12:58
2015-04-01 13:17:23 30307732 04/01/2015 01:17:23 PM 04/01/2015 01:31:22 PM NYPD New York City Police Department Traffic Congestion/Gridlock MANHATTAN 2015-04-01 13:17:23
2015-04-01 21:39:04 30311958 04/01/2015 09:39:04 PM 04/01/2015 09:50:48 PM NYPD New York City Police Department Noise - Vehicle Car/Truck Music BROOKLYN 2015-04-01 21:39:04
2015-04-01 12:53:45 30309365 04/01/2015 12:53:45 PM 04/02/2015 12:04:38 PM DCA Department of Consumer Affairs Consumer Complaint Overcharge QUEENS 2015-04-01 12:53:45
2015-04-01 10:46:01 30312487 04/01/2015 10:46:01 AM 04/02/2015 03:34:31 PM DCA Department of Consumer Affairs Consumer Complaint Damaged/Defective Goods BROOKLYN 2015-04-01 10:46:01

573 rows × 9 columns

What was the most popular type of complaint on April 1st?

What were the most popular three types of complaint on April 1st


In [12]:
print("The most popular three types of complaint on April 1st were:")
c = 1
for value, count in april_1['Complaint Type'].value_counts().iteritems(): # zip type
    if c > 3:
        break
    print("{}) {} ({} complaints)".format(c, value, count))
    c = c + 1


The most popular three types of complaint on April 1st were:
1) Illegal Parking (67 complaints)
2) Street Condition (64 complaints)
3) Blocked Driveway (58 complaints)

In [ ]:

What month has the most reports filed? How many? Graph it.


In [21]:
#mdata = df.resample('M').size()
mdata = df.groupby([lambda x: x.month]).size() # this one also works if we want to merge multiple years

In [22]:
months_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
print(months_list[mdata.idxmax()-1], "has the most reports filed, with", mdata.max(), "reports.")


May has the most reports filed, with 49715 reports.

In [23]:
ax = mdata.plot(kind='bar')
plt.xticks(list(range(0,12)), months_list) # or: ax.set_xticklabels(months_list)
print('Reports filed per month:')


Reports filed per month:

What week of the year has the most reports filed? How many? Graph the weekly complaints.


In [24]:
wdata = df.groupby([lambda x: x.week]).size()

In [25]:
wdata.plot(kind='bar')
fig = plt.gcf()
fig.set_size_inches(18, 7, forward=True)
plt.rcParams.update({'font.size': 14})
plt.rc('ytick', labelsize=12)
plt.ylabel('Complaints')
plt.xlabel('Weeks of the year')


Out[25]:
<matplotlib.text.Text at 0x10c19e588>

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 [26]:
noise_df = df[df['Complaint Type'].str.contains('Noise')]

In [39]:
data_annual = noise_df.resample('M').size()
data_annual.plot(figsize=(12, 5), title="Noise complaints - Annual graph")
plt.xlabel("")
print("")




In [40]:
data_cyclic = noise_df.groupby([lambda x: x.hour]).size()
data_cyclic.plot(grid=True, title="Noise complaints - Cyclic graph")
fig = plt.gcf()
fig.set_size_inches(12, 5)
hours = list(range(0, 24))
hours_str = [str(i) + "h" for i in hours]
plt.xticks(hours, hours_str)
print('')



Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.


In [41]:
ddata = df.groupby([lambda x: x.month, lambda x: x.day]).size()

In [42]:
days_top5 = ddata.sort_values(ascending=False).head(5)
days_top5_list = []
for day, count in days_top5.items():
    day_str = months_list[day[0]-1] + " " + str(day[1])
    print("-", day_str, "with", count, "complaints;")
    days_top5_list.append({"Day": day_str, "Count": count})


- October 28 with 2697 complaints;
- November 9 with 2529 complaints;
- May 4 with 2465 complaints;
- May 11 with 2293 complaints;
- October 29 with 2258 complaints;

In [43]:
top5_df = pd.DataFrame(days_top5_list)
top5_df.plot(kind='bar', x='Day', y='Count', legend=False)


Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x10c471908>

What hour of the day are the most complaints? Graph a day of complaints.


In [44]:
hdata = df.groupby([lambda x: x.hour]).size()
print(hdata.idxmax(), "hour has the most reports filed, with", hdata.max(), "reports filed.")


0 hour has the most reports filed, with 22427 reports filed.

In [45]:
hdata.plot(title="A day of complaints", figsize=(12,5))
plt.xticks(hours, hours_str)
print('')



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 [46]:
# The odd number is midnight (very high peak)

type_per_h = pd.DataFrame(df.groupby([lambda x: x.hour])['Complaint Type'].value_counts())

print("0h, Top Counts of", type_per_h['Complaint Type'][0].head(3))

print("\n23h, Top Counts of", type_per_h['Complaint Type'][23].head(3))

print("\n01h, Top Counts of", type_per_h['Complaint Type'][1].head(3))


0h, Top Counts of Complaint Type
HEAT/HOT WATER    4534
Rodent            2112
PAINT/PLASTER     1946
Name: Complaint Type, dtype: int64

23h, Top Counts of Complaint Type
Noise - Street/Sidewalk    1599
Noise - Commercial         1503
Blocked Driveway            973
Name: Complaint Type, dtype: int64

01h, Top Counts of Complaint Type
Noise - Commercial         1025
Noise - Street/Sidewalk     897
Blocked Driveway            479
Name: Complaint Type, dtype: int64

So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.


In [47]:
df_midnight = df[df.index.hour==0]
df_midnight = df_midnight.groupby(by=df_midnight.index.minute)
df_midnight['Unique Key'].count().plot()


Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x10c3017b8>

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 [48]:
common_agencies = list(df['Agency Name'].value_counts().head(5).keys())
for agency in common_agencies:
    df_agency = df[df['Agency Name'] == agency]
    hdata = df_agency.groupby([lambda x: x.hour]).size()
    hdata.plot(legend=True, figsize=(12,5))
    plt.xticks(hours, hours_str)
plt.legend(common_agencies)
print('')



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 [49]:
for agency in common_agencies:
    df_agency = df[df['Agency Name'] == agency]
    wdata = df_agency.groupby([lambda x: x.week]).size()
    wdata.plot(legend=True, figsize=(15,8))
plt.legend(common_agencies)
print('')



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 [50]:
nypd = common_agencies[0]
hpd = common_agencies[1]

def complaints_for_agency(agency):
    return df[df['Agency Name'] == agency]

print("NYPD July-August:\n" + str(complaints_for_agency(nypd)['2015-07':'2015-08']['Complaint Type'].value_counts().head(10)))
print("\nNYPD May:\n" + str(complaints_for_agency(nypd)['2015-05']['Complaint Type'].value_counts().head(10)))

print("\nHPD Winter:\n" + str(complaints_for_agency(hpd)['2015-06-21':'2015-09-20']['Complaint Type'].value_counts().head(10)))
print("\nHPD Summer:\n" + str(complaints_for_agency(hpd)['2015-01-01':'2015-03-21']['Complaint Type'].value_counts().head(10)))


NYPD July-August:
Illegal Parking            3444
Blocked Driveway           3258
Noise - Street/Sidewalk    3165
Noise - Commercial         1201
Noise - Vehicle             942
Derelict Vehicle            904
Animal Abuse                406
Noise - Park                331
Homeless Encampment         275
Vending                     205
Name: Complaint Type, dtype: int64

NYPD May:
Blocked Driveway           4114
Illegal Parking            3975
Noise - Street/Sidewalk    3385
Noise - Commercial         2263
Noise - Vehicle            1232
Derelict Vehicle           1000
Animal Abuse                417
Vending                     337
Traffic                     335
Homeless Encampment         264
Name: Complaint Type, dtype: int64

HPD Winter:
UNSANITARY CONDITION    291
PAINT/PLASTER           182
PLUMBING                150
WATER LEAK               95
ELECTRIC                 87
FLOORING/STAIRS          86
DOOR/WINDOW              83
GENERAL                  80
HEAT/HOT WATER           55
APPLIANCE                35
Name: Complaint Type, dtype: int64

HPD Summer:
UNSANITARY CONDITION    13
WATER LEAK               3
GENERAL                  3
PAINT/PLASTER            3
PLUMBING                 3
FLOORING/STAIRS          2
APPLIANCE                2
HEAT/HOT WATER           2
Name: Complaint Type, dtype: int64