Professional Documents
Culture Documents
Import As Import As Import Import As Import As Import Import
Import As Import As Import Import As Import As Import Import
import pandas as pd
import matplotlib
import datetime
import calendar
data = pd.read_csv('311_Service_Requests_from_2010_to_Present.csv')
In [3]: data.columns
Out[3]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
In [4]: data.info()
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 1/34
8/3/22, 9:27 PM Project1-Copy1
<class 'pandas.core.frame.DataFrame'>
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 2/34
8/3/22, 9:27 PM Project1-Copy1
In [5]: pd.set_option('display.max_columns',None)
data.sample(4)
Out[5]:
Unique Created Closed Agency Complaint
Agency Descriptor Location
Key Date Date Name Type
data_mod.columns
Out[7]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
In [8]: pd.unique(data['Agency'])
array(['NYPD'], dtype=object)
Out[8]:
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 3/34
8/3/22, 9:27 PM Project1-Copy1
New York City Police Department 300690
Out[9]:
Internal Affairs Bureau 6
NYPD 2
Out[10]:
Illegal Parking 75361
In [11]: data.Descriptor.value_counts().head(5)
Out[11]:
No Access 56976
Street/Sidewalk 249299
Out[12]:
Store/Commercial 20381
Club/Bar/Restaurant 17360
11385.0 5167
Out[13]:
11368.0 4298
11211.0 4225
11234.0 4150
Out[14]:
78-15 PARSONS BOULEVARD 505
BROADWAY 3237
Out[15]:
3 AVENUE 1241
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 4/34
8/3/22, 9:27 PM Project1-Copy1
BROADWAY 4338
Out[16]:
BEND 4129
3 AVENUE 3112
5 AVENUE 3035
BEND 4391
Out[17]:
BROADWAY 3784
8 AVENUE 2766
BROADWAY 672
Out[18]:
170 STREET 441
44 STREET 355
6 AVENUE 348
BROADWAY 1358
Out[19]:
6 AVENUE 715
2 AVENUE 617
5 AVENUE 551
ADDRESS 238644
Out[20]:
INTERSECTION 43366
BLOCKFACE 12014
LATLONG 3509
In [21]: data['City'].value_counts().head(4)
BROOKLYN 98307
Out[21]:
NEW YORK 65994
BRONX 40702
In [22]: data.Landmark.value_counts().head(5)
CENTRAL PARK 67
Out[22]:
PROSPECT PARK 22
SUNSET PARK 13
Precinct 298527
Out[23]:
Name: Facility Type, dtype: int64
In [24]: data.Status.value_counts()
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 5/34
8/3/22, 9:27 PM Project1-Copy1
Closed 298471
Out[24]:
Open 1439
Assigned 786
Draft 2
11-07-15 7:34 9
Out[25]:
06-07-15 6:23 9
07-12-15 7:04 9
11-02-15 6:12 8
The Police Department responded to the complaint and with the information available o
Out[26]:
bserved no evidence of the violation at that time. 90490
The Police Department responded to the complaint and took action to fix the conditio
n. 61624
The Police Department responded and upon arrival those responsible for the condition
were gone. 58031
The Police Department responded to the complaint and determined that police action wa
s not necessary. 38211
Unspecified 300697
Out[27]:
Alley Pond Park - Nature Center 1
Unspecified 300697
Out[28]:
Q001 1
Unspecified 300697
Out[29]:
Name: School Region, dtype: int64
N 300698
Out[30]:
Name: School Not Found, dtype: int64
Unspecified 300697
Out[31]:
Name: School Code, dtype: int64
Unspecified 300697
Out[32]:
7182176034 1
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 6/34
8/3/22, 9:27 PM Project1-Copy1
Unspecified 300697
Out[33]:
Grand Central Parkway, near the soccer field 1
Unspecified 300697
Out[34]:
QUEENS 1
Unspecified 300697
Out[35]:
NY 1
Unspecified 300697
Out[36]:
Name: School Zip, dtype: int64
N 300698
Out[37]:
Name: School Not Found, dtype: int64
In [40]: data.columns
Out[40]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
In [43]: data_mod.columns
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 7/34
8/3/22, 9:27 PM Project1-Copy1
Out[43]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
In [48]: data_mod.columns
Out[48]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
FDR Dr 33
Out[49]:
Belt Pkwy 30
BQE/Gowanus Expwy 27
East/Queens Bound 21
Out[50]:
Northbound/Uptown 20
North/Bronx Bound 20
Roadway 162
Out[51]:
Ramp 51
Out[52]:
Bronx River Pkwy (Exit 4B) - Westchester Ave / White Plains Road (Exit 5A) 5
Westchester Ave / White Plains Road (Exit 5A) - Castle Hill Ave (Exit 5B) 3
BEGIN Staten Island Expwy (Exit 15N) - Lily Pond Ave/Bay St (Exit 15S) 3
Manhattan Bound 1
Out[54]:
Name: Ferry Direction, dtype: int64
Out[55]:
Barberi 1
In [57]: data_mod.columns
Out[57]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
In [58]: data['Latitude'].value_counts().head(5)
40.830362 902
Out[58]:
40.721959 505
40.703819 480
40.647132 362
40.708726 341
In [59]: data['Longitude'].value_counts().head(5)
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 9/34
8/3/22, 9:27 PM Project1-Copy1
-73.866022 902
Out[59]:
-73.809697 505
-73.942073 480
-73.790654 341
-74.004623 340
In [60]: data['Location'].value_counts().head(4)
Out[60]:
(40.72195913199264, -73.80969682426189) 505
In [61]: data_mod.sample(10)
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 10/34
8/3/22, 9:27 PM Project1-Copy1
Out[61]:
Created Closed Agency Complaint
Agency Descriptor Location Type
Date Date Name Type
New York
09-08-15 09-09-15 Blocked
120171 NYPD City Police Illegal Parking Street/Sidewalk
22:05 3:06 Hydrant
Department
New York
08-03-15 08-04-15 Blocked
161462 NYPD City Police No Access Street/Sidewalk
11:41 0:12 Driveway
Department
New York
09-04-15 09-04-15 Noise - Loud
126015 NYPD City Police Street/Sidewalk
10:25 12:32 Street/Sidewalk Music/Party
Department
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 11/34
8/3/22, 9:27 PM Project1-Copy1
In [62]: data_mod.columns
Out[62]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
dtype='object')
In [63]: data_mod.info()
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 12/34
8/3/22, 9:27 PM Project1-Copy1
<class 'pandas.core.frame.DataFrame'>
import pandas as pd
import datetime
data = pd.read_csv('311_Service_Requests_from_2010_to_Present.csv')
data_mod
#data_mod = data_mod[(data_mod.Request_Closing_Time)>=0]
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 13/34
8/3/22, 9:27 PM Project1-Copy1
#df['Created Date'] = pd.to_datetime(df['Created Date'])
data = pd.read_csv('311_Service_Requests_from_2010_to_Present.csv')
In [65]: data_mod.info()
<class 'pandas.core.frame.DataFrame'>
In [66]: data_mod.sample(4)
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 14/34
8/3/22, 9:27 PM Project1-Copy1
Out[66]:
Created Closed Agency Complaint Incid
Agency Descriptor Location Type
Date Date Name Type
Posted
2015- 2015- New York
Parking
77186 10-16 10-17 NYPD City Police Illegal Parking Street/Sidewalk 1123
Sign
23:52:56 02:19:59 Department
Violation
In [67]: data_mod.columns
Out[67]:
'Complaint Type', 'Descriptor', 'Location Type', 'Incident Zip',
'Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2',
'Request_Closing_Time'],
dtype='object')
data_complaint = data_complaint.to_frame()
data_complaint
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 15/34
8/3/22, 9:27 PM Project1-Copy1
Out[68]: Counts
Traffic 4498
Vending 3802
Drinking 1280
Panhandling 307
Graffiti 113
Agency Issues 6
Squeegee 4
Ferry Complaint 2
Animal in a Park 1
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 16/34
8/3/22, 9:27 PM Project1-Copy1
Squeegee 4 0.00
data_complaint = data_complaint.reset_index()
data_complaint
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 17/34
8/3/22, 9:27 PM Project1-Copy1
In [71]: plt.figure(figsize=(12,6))
plt.show()
plt.tight_layout()
data_descriptor = data_descriptor.to_frame()
data_descriptor = data_descriptor.rename(columns={'Descriptor':'Percentage'})
data_descriptor['Descriptor'] = data_descriptor.index
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 18/34
8/3/22, 9:27 PM Project1-Copy1
cols = data_descriptor.columns.tolist()
cols = cols[-1:]+cols[:-1]
data_descriptor = data_descriptor[cols]
data_descriptor = data_descriptor.reset_index()
data_descriptor = data_descriptor.drop(columns=['index'],axis=1)
data_descriptor
1 No Access 19.33
data_location_type = data_location_type.to_frame()
cols = data_location_type.columns.tolist()
cols = cols[-1:]+cols[:-1]
data_location_type = data_location_type[cols]
data_location_type = data_location_type.reset_index()
data_location_type = data_location_type.drop(columns=['index'],axis=1)
data_location_type
0 Street/Sidewalk 82.94
1 Store/Commercial 6.78
2 Club/Bar/Restaurant 5.78
4 Park/Playground 1.59
data_city = data_city.to_frame()
data_city = data_city.rename(columns={'City':'Percentage'})
data_city['City'] = data_city.index
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 19/34
8/3/22, 9:27 PM Project1-Copy1
cols = data_city.columns.tolist()
cols = cols[-1:]+cols[:-1]
data_city = data_city[cols]
data_city = data_city.reset_index()
data_city = data_city.drop(columns=['index'],axis=1)
data_city
0 BROOKLYN 32.98
2 BRONX 13.65
4 JAMAICA 2.45
5 ASTORIA 2.12
6 FLUSHING 2.00
7 RIDGEWOOD 1.73
8 CORONA 1.44
9 WOODSIDE 1.19
data_address_type = data_address_type.to_frame()
cols = data_address_type.columns.tolist()
cols = cols[-1:]+cols[:-1]
data_address_type = data_address_type[cols]
data_address_type = data_address_type.reset_index()
data_address_type = data_address_type.drop(columns=['index'],axis=1)
data_address_type
0 ADDRESS 80.11
1 INTERSECTION 14.56
2 BLOCKFACE 4.03
3 LATLONG 1.18
4 PLACENAME 0.12
descriptor = sns.barplot(ax=ax[0,0],x=data_descriptor.Descriptor,y=data_descriptor.Per
descriptor.set_xticklabels(descriptor.get_xticklabels(), rotation=30, ha="right")
city = sns.barplot(ax=ax[1,0],x=data_city['City'],y=data_city.Percentage,)
plt.tight_layout()
0 2015-12-31 23:59:45
Out[85]:
1 2015-12-31 23:59:44
2 2015-12-31 23:59:29
3 2015-12-31 23:57:46
4 2015-12-31 23:56:58
Month_Day = pd.DataFrame()
Month_Day['Date'] = pd.to_datetime(Year_Month_Day.dt.date)
Month_Day['Month'] = Year_Month_Day.dt.month
Month_Day['Day'] = Year_Month_Day.dt.day
Month_Day['Month Name'] = Month_Day['Month'].apply(lambda x: calendar.month_abbr[x])
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 21/34
8/3/22, 9:27 PM Project1-Copy1
5:'Saturday',6:'Sunday'})
Month_Day.sample(20)
Month_plot = Month_plot.to_frame()
Month_plot
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 22/34
8/3/22, 9:27 PM Project1-Copy1
Out[88]: Counts
May 36437
Sep 35427
Jun 35315
Aug 34956
Jul 34888
Oct 32605
Nov 30773
Dec 30521
Apr 27305
Mar 2471
Day_plot = Day_plot.to_frame()
Day_plot
Out[89]: Counts
Sunday 47969
Saturday 47564
Friday 43995
Thursday 41342
Monday 40489
Wednesday 39788
Tuesday 39551
plt.tight_layout()
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 23/34
8/3/22, 9:27 PM Project1-Copy1
Month_Day_grouped_final.head(15)
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 24/34
8/3/22, 9:27 PM Project1-Copy1
Date 3222
Out[92]:
Month 3222
Day 3222
Day No 3222
dtype: int64
In [93]: plt.figure(figsize=(20,8))
plt.show()
plt.tight_layout()
In [95]: data_mod['Status'].value_counts().plot(kind='barh')
plt.xlabel('Status')
plt.ylabel('Counts')
plt.show()
plt.tight_layout()
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 25/34
8/3/22, 9:27 PM Project1-Copy1
Complaint_AvgTime = Complaint_AvgTime.sort_values(['DeltaT(in_hr.)']).reset_index()
Complaint_AvgTime
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 26/34
8/3/22, 9:27 PM Project1-Copy1
6 Traffic 3.446291
11 Drinking 3.855354
12 Vending 4.013619
13 Squeegee 4.047500
15 Panhandling 4.372852
19 Graffiti 7.151062
Tmean_with = float(Complaint_AvgTime['DeltaT(in_hr.)'].mean())
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 27/34
8/3/22, 9:27 PM Project1-Copy1
T-statistic is = 0.0
p value is = 1.0
In [103… if (pval_with<0.05):
Complaint_AvgTime_without
6 Traffic 3.446291
11 Drinking 3.855354
12 Vending 4.013619
13 Squeegee 4.047500
15 Panhandling 4.372852
19 Graffiti 7.151062
T-statistic is = 0.0
p value is = 1.0
In [106… if (pval_without<0.05):
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 28/34
8/3/22, 9:27 PM Project1-Copy1
sample1
19 Graffiti 7.151062
6 Traffic 3.446291
13 Squeegee 4.047500
12 Vending 4.013619
sample2
11 Drinking 3.855354
15 Panhandling 4.372852
T-statistic is = -0.998538749693149
p value is = 0.33
In [111… if (p_val<0.05):
sample1_anova
11 Drinking 3.855354
12 Vending 4.013619
rest_data
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 30/34
8/3/22, 9:27 PM Project1-Copy1
6 Traffic 3.446291
13 Squeegee 4.047500
15 Panhandling 4.372852
19 Graffiti 7.151062
sample2_anova
13 Squeegee 4.047500
sample3_anova
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 31/34
8/3/22, 9:27 PM Project1-Copy1
6 Traffic 3.446291
15 Panhandling 4.372852
19 Graffiti 7.151062
print('F-statistic is =',f_val)
F-statistic is = 0.4571465849876404
p value is = 0.0
print('F-statistic is =',f_val)
F-statistic is = 0.9116257429122925
p value is = 0.37
print('F-statistic is =',f_val)
F-statistic is = 0.9155988693237305
p value is = 0.44
F-statistic is = 1.0561063615758082
p value is = 0.37
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 32/34
8/3/22, 9:27 PM Project1-Copy1
F-statistic is = 1.0554687602723551
p value is = 0.37
In [122… if (p_val<0.05):
p value is = 0.3
p value is = 0.35
p value is = 0.32
df_cc = df_cc.dropna()
Out[129]:
BREEZY
City ARVERNE ASTORIA Astoria BAYSIDE BELLEROSE BRONX BROOKLYN
POINT
Complaint Type
Animal in a Park 0 0 0 0 0 0 0 0
Bike/Roller/Skate
0 15 0 0 1 0 20 111
Chronic
Blocked
35 2618 116 377 95 3 12755 28148
Driveway
Disorderly Youth 2 3 0 1 2 0 63 72
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 33/34
8/3/22, 9:27 PM Project1-Copy1
In [ ]:
In [ ]:
In [ ]:
localhost:8888/nbconvert/html/Project1-Copy1.ipynb?download=false 34/34