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-2015.csv",nrows=200000) #Importing the rows. 
df.head(5) #Seems to work.


c:\users\harsha devulapalli\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py:2723: DtypeWarning: Columns (8,17,48) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)
Out[2]:
Unique Key Created Date Closed Date Agency Agency Name Complaint Type Descriptor Location Type Incident Zip Incident Address ... Bridge Highway Name Bridge Highway Direction Road Ramp Bridge Highway Segment Garage Lot Name Ferry Direction Ferry Terminal Name Latitude Longitude Location
0 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 NaN 11360 27-16 203 STREET ... NaN NaN NaN NaN NaN NaN NaN 40.773540 -73.788237 (40.773539552542, -73.78823697228408)
1 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 Residential Building/House 10019 200 CENTRAL PARK SOUTH ... NaN NaN NaN NaN NaN NaN NaN 40.767021 -73.979448 (40.76702142171206, -73.97944780718524)
2 31950223 11/09/2015 03:55:09 AM 11/09/2015 08:08:57 AM NYPD New York City Police Department Blocked Driveway No Access Street/Sidewalk 10453 1993 GRAND AVENUE ... NaN NaN NaN NaN NaN NaN NaN 40.852671 -73.910608 (40.85267061877697, -73.91060771362552)
3 31000038 07/03/2015 02:18:32 AM 07/03/2015 07:54:48 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11372 84-16 NORTHERN BOULEVARD ... NaN NaN NaN NaN NaN NaN NaN 40.755774 -73.883262 (40.755773786469966, -73.88326243225418)
4 30995614 07/04/2015 12:03:27 AM 07/04/2015 03:33:09 AM NYPD New York City Police Department Noise - Street/Sidewalk Loud Talking Street/Sidewalk 11216 1057 BERGEN STREET ... NaN NaN NaN NaN NaN NaN NaN 40.676175 -73.951269 (40.67617516102934, -73.9512690004692)

5 rows × 53 columns


In [3]:
def parse_date(str_date): #Using a function to convert the dates into datetime format.
    return dateutil.parser.parse(str_date)

df['created_datetime']=df['Created Date'].apply(parse_date) #Applying it here right away!

In [4]:
df.index = df['created_datetime'] #Making the index the date so that the magic below can happen.

In [5]:
df.head(5) #It worked! :D


Out[5]:
Unique Key Created Date Closed Date Agency Agency Name Complaint Type Descriptor Location Type Incident Zip Incident Address ... Bridge Highway Direction Road Ramp Bridge Highway Segment Garage Lot Name Ferry Direction Ferry Terminal Name Latitude Longitude Location 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 NaN 11360 27-16 203 STREET ... NaN NaN NaN NaN NaN NaN 40.773540 -73.788237 (40.773539552542, -73.78823697228408) 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 Residential Building/House 10019 200 CENTRAL PARK SOUTH ... NaN NaN NaN NaN NaN NaN 40.767021 -73.979448 (40.76702142171206, -73.97944780718524) 2015-07-03 13:26:29
2015-11-09 03:55:09 31950223 11/09/2015 03:55:09 AM 11/09/2015 08:08:57 AM NYPD New York City Police Department Blocked Driveway No Access Street/Sidewalk 10453 1993 GRAND AVENUE ... NaN NaN NaN NaN NaN NaN 40.852671 -73.910608 (40.85267061877697, -73.91060771362552) 2015-11-09 03:55:09
2015-07-03 02:18:32 31000038 07/03/2015 02:18:32 AM 07/03/2015 07:54:48 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11372 84-16 NORTHERN BOULEVARD ... NaN NaN NaN NaN NaN NaN 40.755774 -73.883262 (40.755773786469966, -73.88326243225418) 2015-07-03 02:18:32
2015-07-04 00:03:27 30995614 07/04/2015 12:03:27 AM 07/04/2015 03:33:09 AM NYPD New York City Police Department Noise - Street/Sidewalk Loud Talking Street/Sidewalk 11216 1057 BERGEN STREET ... NaN NaN NaN NaN NaN NaN 40.676175 -73.951269 (40.67617516102934, -73.9512690004692) 2015-07-04 00:03:27

5 rows × 54 columns


In [6]:
df.columns #Loads of Columns, most of which won't be relevant anyways.


Out[6]:
Index(['Unique Key', 'Created Date', 'Closed Date', 'Agency', 'Agency Name',
       'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
       'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
       'Intersection Street 1', 'Intersection Street 2', 'Address Type',
       'City', 'Landmark', 'Facility Type', 'Status', 'Due Date',
       'Resolution Description', 'Resolution Action Updated Date',
       'Community Board', 'Borough', 'X Coordinate (State Plane)',
       'Y Coordinate (State Plane)', 'Park Facility Name', 'Park Borough',
       'School Name', 'School Number', 'School Region', 'School Code',
       'School Phone Number', 'School Address', 'School City', 'School State',
       'School Zip', 'School Not Found', 'School or Citywide Complaint',
       'Vehicle Type', 'Taxi Company Borough', 'Taxi Pick Up Location',
       'Bridge Highway Name', 'Bridge Highway Direction', 'Road Ramp',
       'Bridge Highway Segment', 'Garage Lot Name', 'Ferry Direction',
       'Ferry Terminal Name', 'Latitude', 'Longitude', 'Location',
       'created_datetime'],
      dtype='object')

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


In [7]:
df['Complaint Type'].value_counts().head(5) #Most popular complaints are Blocked Driveway, Illegal Parking and HOT WATER?!


Out[7]:
Blocked Driveway           21779
Illegal Parking            19837
HEAT/HOT WATER             12408
Noise - Street/Sidewalk    11949
Noise - Commercial          9603
Name: Complaint Type, dtype: int64

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


In [46]:
df['Complaint Type'].value_counts().head(5).plot(kind='barh',title='Top 5 Frequent Complaints') # A Horizontal Bar Graph of Complaints.


Out[46]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e3555710>

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


In [9]:
df['Borough'].value_counts() #Counting Complaints in the Boroughs. Using Population Data, We can find out how many complaints are there per capita.


Out[9]:
BROOKLYN         57129
QUEENS           46824
MANHATTAN        42050
BRONX            29610
Unspecified      17000
STATEN ISLAND     7387
Name: Borough, dtype: int64

Populations of Manhattan, Brooklyn, Queens, Bronx and Staten Island in 2015 : 1,644,518, 2,636,735, 2,339,150, 1,455,444 , 474,558


In [74]:
57129/2636735


Out[74]:
0.02166656869196184

In [75]:
46824/2339150


Out[75]:
0.02001752773443345

In [76]:
42050/1644518 #Manhattanites complain the most per capita.


Out[76]:
0.0255698022156036

In [77]:
29610/1455444


Out[77]:
0.020344307304162854

In [78]:
7387/474558 #Staten Island the least.


Out[78]:
0.015566063579162083

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


In [10]:
len(df['2015-03']) #Cases filed in March.


Out[10]:
15025

In [11]:
len(df['2015-05']) #Cases filed in May.


Out[11]:
49715

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 [12]:
df['2015-04-01'] #You would wish these were jokes. THEY ARENT!


Out[12]:
Unique Key Created Date Closed Date Agency Agency Name Complaint Type Descriptor Location Type Incident Zip Incident Address ... Bridge Highway Direction Road Ramp Bridge Highway Segment Garage Lot Name Ferry Direction Ferry Terminal Name Latitude Longitude Location 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 Street/Sidewalk 11234 NaN ... NaN NaN NaN NaN NaN NaN 40.609810 -73.922498 (40.60980966645303, -73.92249759633725) 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 Store/Commercial 11205 700 MYRTLE AVENUE ... NaN NaN NaN NaN NaN NaN 40.694644 -73.955504 (40.694643700748486, -73.95550356170298) 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 Street 11422 245-16 149 AVENUE ... NaN NaN NaN NaN NaN NaN 40.653016 -73.738626 (40.653016256598534, -73.73862588133056) 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 Park 11211 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 NaN 11423 90-71 198 STREET ... NaN NaN NaN NaN NaN NaN 40.714299 -73.761158 (40.71429859671565, -73.76115807774032) 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 Street 10467 862 EAST 213 STREET ... NaN NaN NaN NaN NaN NaN 40.878028 -73.860237 (40.87802828144708, -73.86023734606933) 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 Residential Building/House 11368 107-15 NORTHERN BOULEVARD ... NaN NaN NaN NaN NaN NaN 40.757811 -73.861677 (40.757811195752154, -73.86167714731972) 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 Senior Address 10027 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 Street/Sidewalk 11370 32-51 80 STREET ... NaN NaN NaN NaN NaN NaN 40.756412 -73.887405 (40.75641194675221, -73.88740503059863) 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 Highway NaN NaN ... West/Manhattan Bound Roadway Clearview Expwy (I-295) (Exit 27 S-N) - Utopia... NaN NaN NaN NaN NaN NaN 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 Restaurant/Bar/Deli/Bakery 11215 709 5 AVENUE ... NaN NaN NaN NaN NaN NaN 40.660699 -73.994082 (40.660699296661825, -73.99408169463258) 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 Park 11210 NaN ... NaN NaN NaN NaN NaN NaN 40.621474 -73.950711 (40.62147413119333, -73.95071097029123) 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 NaN 10009 620 EAST 12TH STREET ... NaN NaN NaN NaN NaN NaN 40.727725 -73.978204 (40.72772462544187, -73.97820435916094) 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 Restaurant/Bar/Deli/Bakery 11238 663 FRANKLIN AVENUE ... NaN NaN NaN NaN NaN NaN 40.675746 -73.956122 (40.67574618440852, -73.9561218336512) 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 Highway 10304 NaN ... East/Brooklyn Bound Roadway Clove Rd/Richmond Rd (Exit 13) - Lily Pond Ave... NaN NaN NaN 40.606875 -74.085408 (40.60687536641399, -74.0854077221027) 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 Highway 11432 NaN ... West/Toward Triborough Br Ramp 168th St (Exit 17) NaN NaN NaN 40.719228 -73.791963 (40.71922760413319, -73.791962929951) 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 Restaurant/Bar/Deli/Bakery 10011 140 WEST 13 STREET ... NaN NaN NaN NaN NaN NaN 40.737182 -73.998585 (40.737182358685516, -73.99858548189518) 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 Senior Address 11435 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 Store/Commercial 10003 36 SAINT MARKS PLACE ... NaN NaN NaN NaN NaN NaN 40.728733 -73.988011 (40.72873338955463, -73.98801059255561) 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 Street 11385 NaN ... NaN NaN NaN NaN NaN NaN 40.703414 -73.862854 (40.70341423569781, -73.86285397616253) 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 Restaurant/Bar/Deli/Bakery 10028 1291 LEXINGTON AVENUE ... NaN NaN NaN NaN NaN NaN 40.780069 -73.955158 (40.78006850471446, -73.95515761412761) 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 NYC Street Address NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 Street/Sidewalk 10017 210 EAST 46 STREET ... NaN NaN NaN NaN NaN NaN 40.753104 -73.972096 (40.75310402468627, -73.97209629231209) 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 Highway NaN NaN ... East/Queens Bound Roadway Williamsburg Br / Metropolitan Ave (Exit 32) -... NaN NaN NaN NaN NaN NaN 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 Street/Sidewalk 10033 2284 AMSTERDAM AVENUE ... NaN NaN NaN NaN NaN NaN 40.843149 -73.934539 (40.84314882753921, -73.93453937669832) 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 Senior Address 11355 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 NaN 10455 2997 3 AVENUE ... NaN NaN NaN NaN NaN NaN 40.819111 -73.913908 (40.819110789789214, -73.91390802507868) 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 NaN 11223 1701 WEST 8 STREET ... NaN NaN NaN NaN NaN NaN 40.605657 -73.981194 (40.60565667868274, -73.98119372058547) 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 Senior Address 11201 NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 Street/Sidewalk 11210 650 EAST 24 STREET ... NaN NaN NaN NaN NaN NaN 40.634497 -73.954167 (40.63449684441219, -73.95416735372353) 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 Street 11207 NaN ... NaN NaN NaN NaN NaN NaN 40.679561 -73.898899 (40.67956105192572, -73.89889884573184) 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 Street 11203 NaN ... NaN NaN NaN NaN NaN NaN 40.658529 -73.939568 (40.6585289219231, -73.93956820621213) 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 Street 11234 J AVENUE ... NaN NaN NaN NaN NaN NaN 40.628542 -73.921838 (40.62854243316789, -73.92183818389044) 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 NaN 10001 511 WEST 25 STREET ... NaN NaN NaN NaN NaN NaN 40.749380 -74.004169 (40.74937996228322, -74.00416853967121) 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 NaN 10032 NaN ... NaN NaN NaN NaN NaN NaN 40.842154 -73.942278 (40.84215388602991, -73.94227827092928) 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 Street 10307 647 CRAIG AVENUE ... NaN NaN NaN NaN NaN NaN 40.506708 -74.252182 (40.50670803830861, -74.25218246259357) 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 NaN 11369 22-19 93 STREET ... NaN NaN NaN NaN NaN NaN 40.769574 -73.877480 (40.769573850244676, -73.8774799367093) 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 Street 10021 NaN ... NaN NaN NaN NaN NaN NaN 40.765546 -73.954702 (40.765545913197165, -73.95470170187454) 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 Street 11229 2056 EAST 29 STREET ... NaN NaN NaN NaN NaN NaN 40.601403 -73.943106 (40.60140342407911, -73.94310580244269) 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 Street NaN NaN ... NaN NaN NaN NaN NaN NaN 40.720103 -73.790376 (40.72010305201917, -73.79037648278602) 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 Street 11209 NaN ... NaN NaN NaN NaN NaN NaN 40.633428 -74.032876 (40.63342806685948, -74.03287604669814) 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 Street 11217 90 PROSPECT PLACE ... NaN NaN NaN NaN NaN NaN 40.679040 -73.974579 (40.67903998236064, -73.97457889877462) 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 Highway NaN NaN ... West/Staten Island Bound Roadway Crospey Ave Stillwell Ave (Exit 6N) - Crospey ... NaN NaN NaN NaN NaN NaN 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 NaN 11429 214-16 110 AVENUE ... NaN NaN NaN NaN NaN NaN 40.708131 -73.743041 (40.70813050331176, -73.74304104617282) 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 Address Outside of NYC NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 NYC Street Address NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 NYC Street Address NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 NYC Street Address NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 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 Street/Sidewalk 11421 85-86 87 STREET ... NaN NaN NaN NaN NaN NaN 40.694888 -73.857927 (40.69488849346232, -73.85792744070989) 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 Street 10451 215 EAST 161 STREET ... NaN NaN NaN NaN NaN NaN 40.826235 -73.920529 (40.8262353417949, -73.92052920426786) 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 Restaurant/Bar/Deli/Bakery 11234 2301 FLATBUSH AVENUE ... NaN NaN NaN NaN NaN NaN 40.613889 -73.927186 (40.61388875283825, -73.92718600732812) 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 Street 10128 NaN ... NaN NaN NaN NaN NaN NaN 40.781533 -73.958320 (40.78153263581957, -73.9583197488706) 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 Restaurant/Bar/Deli/Bakery 11377 59-21 ROOSEVELT AVENUE ... NaN NaN NaN NaN NaN NaN 40.745586 -73.904573 (40.74558568959288, -73.90457292624892) 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 Street/Sidewalk 11691 348 BEACH 40 STREET ... NaN NaN NaN NaN NaN NaN 40.595019 -73.772153 (40.5950185756628, -73.77215306630436) 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 Club/Bar/Restaurant 10013 301 CHURCH STREET ... NaN NaN NaN NaN NaN NaN 40.719322 -74.004470 (40.71932215308254, -74.00446968948569) 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 Street/Sidewalk 10306 200 ADELAIDE AVENUE ... NaN NaN NaN NaN NaN NaN 40.561690 -74.124622 (40.5616902523158, -74.12462211525013) 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 Street/Sidewalk 10013 NaN ... NaN NaN NaN NaN NaN NaN 40.720557 -74.003510 (40.72055732795014, -74.00351016018516) 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 Street/Sidewalk 11207 184 JEROME STREET ... NaN NaN NaN NaN NaN NaN 40.677739 -73.887888 (40.677739297670584, -73.8878875660618) 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 NaN 11418 NaN ... NaN NaN NaN NaN NaN NaN 40.700108 -73.832667 (40.70010803283339, -73.83266746664873) 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 NaN 11232 807 42 STREET ... NaN NaN NaN NaN NaN NaN 40.645348 -73.998616 (40.64534787518196, -73.99861625677346) 2015-04-01 10:46:01

573 rows × 54 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 [13]:
df['2015-04-01']['Complaint Type'].value_counts().head(3) #Illegal Parking is NOT A JOKE!


Out[13]:
Illegal Parking     67
Street Condition    64
Blocked Driveway    58
Name: Complaint Type, dtype: int64

In [ ]:

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


In [47]:
df['Complaint Type'].resample('M').count().plot(figsize=(20,8),title='Complaints Per Month in 2015') #Using the resampler and tada!


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

There seems to be a peak of complaints in May and October - at the beginning of summer and winter. Coincidence?!

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


In [48]:
df['Complaint Type'].resample('W').count().plot(figsize=(20,8),title='Complaints Per Week in 2015.')


Out[48]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e3c2ee80>

A little more nuanced than the earlier month graph.

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

In [85]:
noise_df['created_datetime'].resample('M').count().plot()


Out[85]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9ec8f8400>

In [87]:
noise_df['created_datetime'].resample('H').count().plot()


Out[87]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9eca8ab70>

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


In [49]:
list = df['Complaint Type'].resample('D').count()
list.sort_values(ascending=False).head().plot(kind='barh',title='Top Days to Complain in 2015.')


Out[49]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e3f2fc88>

In [ ]:

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


In [51]:
df['2015-10-28']['Complaint Type'].resample('H').count().plot(figsize=(20,8),title='What time of the day are most complaints being reported')


Out[51]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e95fc4a8>

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 [52]:
df['2015-10-28 16']['Complaint Type'].value_counts().head(5) #Something seems wrong with 4 o clock that day.


Out[52]:
HEAT/HOT WATER          29
UNSANITARY CONDITION    18
PAINT/PLASTER           17
Illegal Parking         17
WATER LEAK              10
Name: Complaint Type, dtype: int64

In [ ]:


In [ ]:

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


In [53]:
df['2015-10-28 00']['Complaint Type'].resample('T').count().plot(figsize=(20,8),title='Per Minute Breakdown of Complaints between 12 AM and 1 AM')


Out[53]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e965b320>

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 [20]:
df['Agency Name'].value_counts().head(5) #Top 5 Agencies that handle complaints for 311.


Out[20]:
New York City Police Department                       80000
Department of Housing Preservation and Development    39363
Department of Transportation                          22308
Department of Parks and Recreation                    15128
Department of Health and Mental Hygiene                8216
Name: Agency Name, dtype: int64

In [21]:
def parse_date_into_time(str_date): #So for us to be able to handle the next questions. I create a function that converts 
                                    #time into a HHMM format so that the graphs below can be smoother. 
    
    h=str(dateutil.parser.parse(str_date).hour)
    m=str(dateutil.parser.parse(str_date).minute)
    t=h+":"+m
    return dateutil.parser.parse(t).time()

In [22]:
parse_date_into_time('07/06/2015 10:58:27 AM') #Seems to be working.


Out[22]:
datetime.time(10, 58)

In [23]:
df['Upload Time']=df['Created Date'].apply(parse_date_into_time) #create a new column called Upload Time.

In [24]:
df.head(5) #Working.


Out[24]:
Unique Key Created Date Closed Date Agency Agency Name Complaint Type Descriptor Location Type Incident Zip Incident Address ... Road Ramp Bridge Highway Segment Garage Lot Name Ferry Direction Ferry Terminal Name Latitude Longitude Location created_datetime Upload Time
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 NaN 11360 27-16 203 STREET ... NaN NaN NaN NaN NaN 40.773540 -73.788237 (40.773539552542, -73.78823697228408) 2015-07-06 10:58:27 10:58:00
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 Residential Building/House 10019 200 CENTRAL PARK SOUTH ... NaN NaN NaN NaN NaN 40.767021 -73.979448 (40.76702142171206, -73.97944780718524) 2015-07-03 13:26:29 13:26:00
2015-11-09 03:55:09 31950223 11/09/2015 03:55:09 AM 11/09/2015 08:08:57 AM NYPD New York City Police Department Blocked Driveway No Access Street/Sidewalk 10453 1993 GRAND AVENUE ... NaN NaN NaN NaN NaN 40.852671 -73.910608 (40.85267061877697, -73.91060771362552) 2015-11-09 03:55:09 03:55:00
2015-07-03 02:18:32 31000038 07/03/2015 02:18:32 AM 07/03/2015 07:54:48 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11372 84-16 NORTHERN BOULEVARD ... NaN NaN NaN NaN NaN 40.755774 -73.883262 (40.755773786469966, -73.88326243225418) 2015-07-03 02:18:32 02:18:00
2015-07-04 00:03:27 30995614 07/04/2015 12:03:27 AM 07/04/2015 03:33:09 AM NYPD New York City Police Department Noise - Street/Sidewalk Loud Talking Street/Sidewalk 11216 1057 BERGEN STREET ... NaN NaN NaN NaN NaN 40.676175 -73.951269 (40.67617516102934, -73.9512690004692) 2015-07-04 00:03:27 00:03:00

5 rows × 55 columns


In [25]:
nypddf=df[df['Agency Name']=='New York City Police Department']

In [54]:
nypddf['Upload Time'].value_counts().plot(figsize=(20,8),title='Time of the Day NYPD files its reports')


Out[54]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e9f476d8>

In [27]:
dphpdf=df[df['Agency Name']=='Department of Housing Preservation and Development']

In [28]:
dphpdf['Upload Time'].value_counts().head(10) #ALL UPLOADED AT MIDNIGHT, by some unpaid intern.


Out[28]:
00:00:00    14473
13:34:00       57
12:20:00       56
10:10:00       56
13:20:00       55
13:07:00       54
15:38:00       53
11:09:00       52
12:05:00       52
12:09:00       51
Name: Upload Time, dtype: int64

In [29]:
dotdf=df[df['Agency Name']=='Department of Transportation']

In [55]:
dotdf['Upload Time'].value_counts().plot(figsize=(20,8),title='Time of the Day DOT files its reports')


Out[55]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e383e240>

It looks like they file reports just before lunch time and before they leave office?!


In [31]:
doprdf=df[df['Agency Name']=='Department of Parks and Recreation']

In [56]:
doprdf['Upload Time'].value_counts().plot(figsize=(20,8),title='Time of the Day DoP&R files its reports')


Out[56]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e9baa940>

They seem to do it throughout the day.


In [33]:
dohmhdf=df[df['Agency Name']=='Department of Health and Mental Hygiene']

In [57]:
dohmhdf['Upload Time'].value_counts().plot(figsize=(20,8),title='Time of the Day DoHMH files its reports')


Out[57]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9e9e72320>

Only at Midnight, they upload their data.


In [35]:
df['Complaint Type'].resample('W').count().plot(figsize=(20,8))


Out[35]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9d624e240>

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 [58]:
nypddf['Complaint Type'].resample('W').count().plot(figsize=(20,8),title='NYPD: Complaints Per Week')


Out[58]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9ea651940>

In [59]:
dphpdf['Complaint Type'].resample('W').count().plot(figsize=(20,8),title='DPHP: Complaints Per Week')


Out[59]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9ea9469e8>

In [60]:
dotdf['Complaint Type'].resample('W').count().plot(figsize=(20,8),title='DOT: Complaints Per Week')


Out[60]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9eac62b38>

In [61]:
doprdf['Complaint Type'].resample('W').count().plot(figsize=(20,8),title='DOPR: Complaints Per Week')


Out[61]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9eac675f8>

In [62]:
dohmhdf['Complaint Type'].resample('W').count().plot(figsize=(20,8),title='DOHMD: Complaints Per Week')


Out[62]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e9eb2585c0>

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 [41]:
nypddf['2015-07']['Complaint Type'].value_counts().head() #Most Popular Complaints in July


Out[41]:
Illegal Parking            1988
Blocked Driveway           1889
Noise - Street/Sidewalk    1698
Noise - Commercial          659
Derelict Vehicle            535
Name: Complaint Type, dtype: int64

In [42]:
nypddf['2015-08']['Complaint Type'].value_counts().head()#Most Popular Complaints in August


Out[42]:
Noise - Street/Sidewalk    1467
Illegal Parking            1456
Blocked Driveway           1369
Noise - Commercial          542
Noise - Vehicle             409
Name: Complaint Type, dtype: int64

In [43]:
nypddf['2015-05']['Complaint Type'].value_counts().head() #Most Popular Complaints in May


Out[43]:
Blocked Driveway           4114
Illegal Parking            3975
Noise - Street/Sidewalk    3385
Noise - Commercial         2263
Noise - Vehicle            1232
Name: Complaint Type, dtype: int64

In [44]:
dphpdf['2015-06':'2015-08']['Complaint Type'].value_counts().head() #Most Popular Complaints in the Summer


Out[44]:
HEAT/HOT WATER            615
UNSANITARY CONDITION      509
HPD Literature Request    462
PAINT/PLASTER             441
PLUMBING                  308
Name: Complaint Type, dtype: int64

In [45]:
dphpdf['2015-11':'2016-01']['Complaint Type'].value_counts().head() #Most Popular Complaints in the Winter.


Out[45]:
HEAT/HOT WATER          3189
UNSANITARY CONDITION     713
PAINT/PLASTER            598
PLUMBING                 532
DOOR/WINDOW              397
Name: Complaint Type, dtype: int64

In [ ]: