Professional Documents
Culture Documents
In [1]:
import numpy as np
import pandas as pd
In [2]:
dataset=pd.read_csv("311_Service_Requests_from_2010_to_Present.csv")
pd.options.display.max_columns = None
dataset.head()
Out[2]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 1/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [3]:
dataset.info()
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 2/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
<class 'pandas.core.frame.DataFrame'>
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 3/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [4]:
dataset.dtypes
Out[4]:
Agency object
Descriptor object
City object
Landmark object
Status object
Borough object
Latitude float64
Longitude float64
Location object
dtype: object
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 4/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
2A:Read or convert the columns ‘Created Date’ and Closed Date’ to datetime
datatype
In [5]:
In [6]:
dataset.head()
Out[6]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 5/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [7]:
dataset.head()
Out[7]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 6/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [8]:
dataset.columns
Out[8]:
'Request_Closing_Time'],
dtype='object')
In [9]:
dataset.drop(['Incident Address', 'Street Name', 'Cross Street 1', 'Cross Street 2','In
tersection Street 1', 'Intersection Street 2',
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 7/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [10]:
dataset.head()
Out[10]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 8/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [11]:
dataset.info()
<class 'pandas.core.frame.DataFrame'>
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 9/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [12]:
dataset.isna().sum()
Out[12]:
Created Date 0
Closed Date 22
Agency 1
Agency Name 1
Complaint Type 1
Descriptor 88
Location Type 1
Incident Zip 30
Address Type 31
City 30
Landmark 4972
Facility Type 21
Status 1
Due Date 1
Borough 1
Park Borough 1
Latitude 40
Longitude 40
Location 40
Request_Closing_Time 22
dtype: int64
In [13]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 10/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [14]:
dataset.head()
Out[14]:
New York
Noise - Loud
0 NYPD City Police Street/Sidewalk 10034.0 ADDRESS NEW
Street/Sidewalk Music/Party
Department
New York
Blocked
1 NYPD City Police No Access Street/Sidewalk 11105.0 ADDRESS AS
Driveway
Department
New York
Blocked
2 NYPD City Police No Access Street/Sidewalk 10458.0 ADDRESS B
Driveway
Department
New York
Blocked
4 NYPD City Police Illegal Parking Street/Sidewalk 11373.0 ADDRESS ELM
Sidewalk
Department
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 11/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [15]:
dataset.isna().sum()
Out[15]:
Agency 1
Agency Name 1
Complaint Type 1
Descriptor 88
Location Type 1
Incident Zip 30
Address Type 31
City 30
Landmark 4972
Facility Type 21
Status 1
Due Date 1
Borough 1
Park Borough 1
Latitude 40
Longitude 40
Location 40
Request_Closing_Time 22
dtype: int64
In [16]:
dataset['Agency'].value_counts()
dataset['Agency Name'].value_counts()
sns.countplot(dataset['Agency Name'])
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[16]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 12/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [17]:
dataset['Complaint Type'].value_counts().head()
plot=sns.countplot(dataset['Complaint Type'])
plot.set_xticklabels(plot.get_xticklabels(),rotation=90)
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[17]:
Text(9, 0, 'Vending'),
Text(10, 0, 'Traffic'),
Text(11, 0, 'Drinking'),
Text(13, 0, 'Panhandling'),
Text(17, 0, 'Graffiti'),
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 13/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [18]:
dataset['Descriptor'].isna().sum()
Out[18]:
88
In [19]:
dataset['Descriptor'].describe()
Out[19]:
count 4887
unique 38
top No Access
freq 1268
In [20]:
dataset['Descriptor'].value_counts().head(5)
Out[20]:
No Access 1268
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 14/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [21]:
plot2=sns.countplot(dataset['Descriptor'])
plot2.set_xticklabels(plot2.get_xticklabels(),rotation=90)
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 15/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[21]:
Text(11, 0, 'Vehicle'),
Text(13, 0, 'Banging/Pounding'),
Text(15, 0, 'Tortured'),
Text(17, 0, 'Congestion/Gridlock'),
Text(18, 0, 'Neglected'),
Text(24, 0, 'Unlicensed'),
Text(32, 0, 'Chained'),
Text(33, 0, 'Building'),
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 16/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [22]:
dataset['Location Type'].isna().sum()
Out[22]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 17/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [23]:
dataset['Location Type'].value_counts().head()
Out[23]:
Street/Sidewalk 4173
Store/Commercial 380
Club/Bar/Restaurant 270
Park/Playground 14
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 18/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [24]:
plot3=sns.countplot(dataset['Location Type'])
plot3.set_xticklabels(plot3.get_xticklabels(),rotation=90)
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 19/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[24]:
[Text(0, 0, 'Street/Sidewalk'),
Text(1, 0, 'Club/Bar/Restaurant'),
Text(2, 0, 'Store/Commercial'),
Text(6, 0, 'Park/Playground'),
Text(9, 0, 'Highway'),
Text(10, 0, 'Commercial')]
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 20/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [25]:
dataset['Incident Zip'].value_counts().head()
dataset['Incident Zip'].isna().sum()
dataset['Incident Zip'].fillna(value=11385,inplace=True)
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 21/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [26]:
dataset['Address Type'].value_counts()
dataset['Address Type'].fillna(value='Address',inplace=True)
sns.countplot(dataset['Address Type'])
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[26]:
In [27]:
In [37]:
dataset.isna().sum()
Out[37]:
Agency 1
Agency Name 1
Complaint Type 1
Descriptor 88
Location Type 0
Incident Zip 0
Address Type 0
City 0
Facility Type 21
Status 1
Due Date 1
Borough 1
Park Borough 1
Request_Closing_Time 0
dtype: int64
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 22/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [39]:
dataset['City'].value_counts().head()
dataset['Facility Type'].value_counts().head()
Out[39]:
Precinct 4954
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 23/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [40]:
dataset['City'].fillna(value='BROOKLYN',inplace=True)
dataset['City'].value_counts().head()
plot4=sns.countplot(x=dataset['City'])
plot4.set_xticklabels(plot4.get_xticklabels(),rotation=90)
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 24/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
Out[40]:
Text(1, 0, 'ASTORIA'),
Text(2, 0, 'BRONX'),
Text(3, 0, 'ELMHURST'),
Text(4, 0, 'BROOKLYN'),
Text(10, 0, 'JAMAICA'),
Text(12, 0, 'RIDGEWOOD'),
Text(18, 0, 'WOODHAVEN'),
Text(19, 0, 'FLUSHING'),
Text(20, 0, 'CORONA'),
Text(23, 0, 'HOLLIS'),
Text(24, 0, 'MASPETH'),
Text(27, 0, 'WOODSIDE'),
Text(33, 0, 'BAYSIDE'),
Text(36, 0, 'BELLEROSE'),
Text(39, 0, 'ROSEDALE'),
Text(40, 0, 'SUNNYSIDE'),
Text(41, 0, 'WHITESTONE'),
Text(42, 0, 'ARVERNE')]
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 25/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [41]:
dataset.isna().sum()
Out[41]:
Agency 1
Agency Name 1
Complaint Type 1
Descriptor 88
Location Type 0
Incident Zip 0
Address Type 0
City 0
Facility Type 21
Status 1
Due Date 1
Borough 1
Park Borough 1
Request_Closing_Time 0
dtype: int64
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 26/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [42]:
dataset['Request_Closing_Time'].head()
Out[42]:
0 0 days 00:55:15
1 0 days 01:26:16
2 0 days 04:51:31
3 0 days 07:45:14
4 0 days 03:27:02
In [43]:
dataset['Request_Closing_Time'].fillna(value=dataset['Request_Closing_Time'].mean(),inp
lace=True)
In [44]:
dataset['Request_Closing_Time'].isna().sum()
Out[44]:
In [48]:
dataset['Request_Closing_Time'].dtypes
Out[48]:
dtype('<m8[ns]')
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 27/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [49]:
dataset.head(10)
Out[49]:
New York
Noise - Loud
0 NYPD City Police Street/Sidewalk 10034.0 ADDRESS NEW
Street/Sidewalk Music/Party
Department
New York
Blocked
1 NYPD City Police No Access Street/Sidewalk 11105.0 ADDRESS AS
Driveway
Department
New York
Blocked
2 NYPD City Police No Access Street/Sidewalk 10458.0 ADDRESS B
Driveway
Department
New York
Blocked
4 NYPD City Police Illegal Parking Street/Sidewalk 11373.0 ADDRESS ELM
Sidewalk
Department
Posted
New York
Parking
5 NYPD City Police Illegal Parking Street/Sidewalk 11215.0 ADDRESS BRO
Sign
Department
Violation
New York
Blocked
6 NYPD City Police Illegal Parking Street/Sidewalk 10032.0 ADDRESS NEW
Hydrant
Department
New York
Blocked
7 NYPD City Police No Access Street/Sidewalk 10457.0 ADDRESS B
Driveway
Department
Posted
New York
Parking
8 NYPD City Police Illegal Parking Street/Sidewalk 11415.0 ADDRESS
Sign GAR
Department
Violation
New York
Blocked
9 NYPD City Police No Access Street/Sidewalk 11219.0 ADDRESS BRO
Driveway
Department
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 28/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [50]:
dataset['Status'].value_counts()
Out[50]:
Closed 4953
Open 16
Assigned 5
In [51]:
sns.countplot(dataset['Status'])
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[51]:
<AxesSubplot:xlabel='Status', ylabel='count'>
In [52]:
#bivariate analysis
dataset['Complaint Type'].value_counts().head(6)
Out[52]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 29/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [53]:
dataset.head(3)
Out[53]:
New York
Noise - Loud N
0 NYPD City Police Street/Sidewalk 10034.0 ADDRESS
Street/Sidewalk Music/Party YO
Department
New York
Blocked
1 NYPD City Police No Access Street/Sidewalk 11105.0 ADDRESS ASTO
Driveway
Department
New York
Blocked
2 NYPD City Police No Access Street/Sidewalk 10458.0 ADDRESS BRO
Driveway
Department
In [54]:
desc=dataset.groupby(by='Complaint Type')['Descriptor'].agg('count')
desc
Out[54]:
Complaint Type
Bike/Roller/Skate Chronic 0
Disorderly Youth 1
Drinking 18
Graffiti 1
Homeless Encampment 0
Noise - Park 9
Panhandling 0
Posting Advertisement 31
Traffic 42
Urinating in Public 0
Vending 96
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 30/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [55]:
dataset.loc[dataset['City']=='NEW YORK',]['Borough'].value_counts()
Out[55]:
MANHATTAN 885
In [56]:
#Newyork city has how many boroughs and whats their status
sns.countplot(x=dataset.loc[dataset['City']=='NEW YORK',]['Borough'],hue='Status',data=
dataset)
Out[56]:
<AxesSubplot:xlabel='Borough', ylabel='count'>
In [57]:
Out[57]:
Vending 74
Noise - Vehicle 58
Homeless Encampment 55
Blocked Driveway 28
Animal Abuse 25
Derelict Vehicle 11
Traffic 11
Noise - Park 4
Panhandling 3
Drinking 2
Urinating in Public 2
Bike/Roller/Skate Chronic 1
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 31/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [58]:
#Countplot to show Newyork city has max complaints of which complaint type?
plot.set_xticklabels(plot.get_xticklabels(),rotation = 90)
Out[58]:
Text(5, 0, 'Vending'),
Text(7, 0, 'Panhandling'),
Text(11, 0, 'Traffic'),
Text(13, 0, 'Drinking'),
In [59]:
dataset.loc[(dataset['City']=='NEW YORK')&(dataset['Status']=='Closed'),:]['Request_Clo
sing_Time'].mean()
Out[59]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 32/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [60]:
dataset.loc[(dataset['City']=='NEW YORK')&(dataset['Status']=='Closed'),:]['Request_Clo
sing_Time'].std()
Out[60]:
In [61]:
dataset['Borough'].value_counts()
Out[61]:
BROOKLYN 1641
QUEENS 1495
MANHATTAN 885
BRONX 684
Unspecified 29
In [62]:
dataset['Location Type'].value_counts()
Out[62]:
Street/Sidewalk 4174
Store/Commercial 380
Club/Bar/Restaurant 270
Park/Playground 14
Residential Building 8
House of Worship 7
Vacant Lot 2
Highway 1
Commercial 1
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 33/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [63]:
sns.countplot(dataset.loc[dataset['Location Type'].isin(['Street/Sidewalk','Store/Comme
rcial','Club/Bar/Restaurant'])]
['Location Type'],data=dataset,hue='Borough')
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[63]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 34/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [64]:
import datetime
dataset.head()
Out[64]:
New York
Noise - Loud
0 NYPD City Police Street/Sidewalk 10034.0 ADDRESS NEW
Street/Sidewalk Music/Party
Department
New York
Blocked
1 NYPD City Police No Access Street/Sidewalk 11105.0 ADDRESS AS
Driveway
Department
New York
Blocked
2 NYPD City Police No Access Street/Sidewalk 10458.0 ADDRESS B
Driveway
Department
New York
Blocked
4 NYPD City Police Illegal Parking Street/Sidewalk 11373.0 ADDRESS ELM
Sidewalk
Department
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 35/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [65]:
sns.countplot(dataset['year'],hue='Borough',data=dataset)
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[65]:
<AxesSubplot:xlabel='year', ylabel='count'>
In [66]:
dataset['Location Type'].value_counts()
Out[66]:
Street/Sidewalk 4174
Store/Commercial 380
Club/Bar/Restaurant 270
Park/Playground 14
Residential Building 8
House of Worship 7
Vacant Lot 2
Highway 1
Commercial 1
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 36/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [67]:
dataset[['Complaint Type','City']].head()
Out[67]:
In [68]:
dataset['Complaint Type'].value_counts()[0:10,]
Out[68]:
Vending 96
Homeless Encampment 75
Traffic 42
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 37/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [69]:
plot3=sns.countplot(dataset['Complaint Type'])
plot3.set_xticklabels(plot3.get_xticklabels(),rotation =90)
/usr/local/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWa
rning: Pass the following variable as a keyword arg: x. From version 0.12,
the only valid positional argument will be `data`, and passing other argum
ents without an explicit keyword will result in an error or misinterpretat
ion.
FutureWarning
Out[69]:
Text(9, 0, 'Vending'),
Text(10, 0, 'Traffic'),
Text(11, 0, 'Drinking'),
Text(13, 0, 'Panhandling'),
Text(17, 0, 'Graffiti'),
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 38/39
7/30/22, 10:51 PM Assignment 1_NYC 311 service request
In [70]:
series=dataset['Complaint Type'].value_counts()[0:5,]
series.nlargest().index
Out[70]:
dtype='object')
In [71]:
#graph
plot4=sns.barplot(x=series.nlargest().index,y=series.nlargest().values)
plot4.set_xticklabels(plot4.get_xticklabels(),rotation =90)
Out[71]:
https://lms.simplilearn.com/courses/2772/Data-Science-with-Python/practice-labs 39/39