10 분 소요

Data Handling

Problem 2.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Problem 3.

from google.colab import drive
drive.mount('/content/gdrive')
Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
file_path = "/content/gdrive/My Drive/Colab Notebooks/311-service-requests.csv"
our_na_values = ['Unspecified', 'N/A']
data = pd.read_csv(file_path, na_values = our_na_values)
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py:2718: DtypeWarning: Columns (8) have mixed types.Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)

Problem 4.

data.head(10)
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 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
0 26589651 10/31/2013 02:08:41 AM NaN NYPD New York City Police Department Noise - Street/Sidewalk Loud Talking Street/Sidewalk 11432 90-03 169 STREET 169 STREET 90 AVENUE 91 AVENUE NaN NaN ADDRESS JAMAICA NaN Precinct Assigned 10/31/2013 10:08:41 AM 10/31/2013 02:35:17 AM 12 QUEENS QUEENS 1042027.0 197389.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.708275 -73.791604 (40.70827532593202, -73.79160395779721)
1 26593698 10/31/2013 02:01:04 AM NaN NYPD New York City Police Department Illegal Parking Commercial Overnight Parking Street/Sidewalk 11378 58 AVENUE 58 AVENUE 58 PLACE 59 STREET NaN NaN BLOCKFACE MASPETH NaN Precinct Open 10/31/2013 10:01:04 AM NaN 05 QUEENS QUEENS 1009349.0 201984.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.721041 -73.909453 (40.721040535628305, -73.90945306791765)
2 26594139 10/31/2013 02:00:24 AM 10/31/2013 02:40:32 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 10032 4060 BROADWAY BROADWAY WEST 171 STREET WEST 172 STREET NaN NaN ADDRESS NEW YORK NaN Precinct Closed 10/31/2013 10:00:24 AM 10/31/2013 02:39:42 AM 12 MANHATTAN MANHATTAN 1001088.0 246531.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.843330 -73.939144 (40.84332975466513, -73.93914371913482)
3 26595721 10/31/2013 01:56:23 AM 10/31/2013 02:21:48 AM NYPD New York City Police Department Noise - Vehicle Car/Truck Horn Street/Sidewalk 10023 WEST 72 STREET WEST 72 STREET COLUMBUS AVENUE AMSTERDAM AVENUE NaN NaN BLOCKFACE NEW YORK NaN Precinct Closed 10/31/2013 09:56:23 AM 10/31/2013 02:21:10 AM 07 MANHATTAN MANHATTAN 989730.0 222727.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.778009 -73.980213 (40.7780087446372, -73.98021349023975)
4 26590930 10/31/2013 01:53:44 AM NaN DOHMH Department of Health and Mental Hygiene Rodent Condition Attracting Rodents Vacant Lot 10027 WEST 124 STREET WEST 124 STREET LENOX AVENUE ADAM CLAYTON POWELL JR BOULEVARD NaN NaN BLOCKFACE NEW YORK NaN NaN Pending 11/30/2013 01:53:44 AM 10/31/2013 01:59:54 AM 10 MANHATTAN MANHATTAN 998815.0 233545.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.807691 -73.947387 (40.80769092704951, -73.94738703491433)
5 26592370 10/31/2013 01:46:52 AM NaN NYPD New York City Police Department Noise - Commercial Banging/Pounding Club/Bar/Restaurant 11372 37 AVENUE 37 AVENUE 84 STREET 85 STREET NaN NaN BLOCKFACE JACKSON HEIGHTS NaN Precinct Open 10/31/2013 09:46:52 AM NaN 03 QUEENS QUEENS 1016948.0 212540.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.749989 -73.881988 (40.7499893014072, -73.88198770727831)
6 26595682 10/31/2013 01:46:40 AM NaN NYPD New York City Police Department Blocked Driveway No Access Street/Sidewalk 11419 107-50 109 STREET 109 STREET 107 AVENUE 109 AVENUE NaN NaN ADDRESS SOUTH RICHMOND HILL NaN Precinct Assigned 10/31/2013 09:46:40 AM 10/31/2013 01:59:51 AM 10 QUEENS QUEENS 1030919.0 187622.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.681533 -73.831737 (40.68153278675525, -73.83173699701601)
7 26595195 10/31/2013 01:44:19 AM 10/31/2013 01:58:49 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11417 137-09 CROSSBAY BOULEVARD CROSSBAY BOULEVARD PITKIN AVENUE VAN WICKLEN ROAD NaN NaN ADDRESS OZONE PARK NaN Precinct Closed 10/31/2013 09:44:19 AM 10/31/2013 01:58:49 AM 10 QUEENS QUEENS 1027776.0 184076.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.671816 -73.843092 (40.67181584567338, -73.84309181950769)
8 26590540 10/31/2013 01:44:14 AM 10/31/2013 02:28:04 AM NYPD New York City Police Department Noise - Commercial Loud Talking Club/Bar/Restaurant 10011 258 WEST 15 STREET WEST 15 STREET 7 AVENUE 8 AVENUE NaN NaN ADDRESS NEW YORK NaN Precinct Closed 10/31/2013 09:44:14 AM 10/31/2013 02:00:56 AM 04 MANHATTAN MANHATTAN 984031.0 208847.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.739913 -74.000790 (40.73991339303542, -74.00079028612932)
9 26594392 10/31/2013 01:34:41 AM 10/31/2013 02:23:51 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11225 835 NOSTRAND AVENUE NOSTRAND AVENUE UNION STREET PRESIDENT STREET NaN NaN ADDRESS BROOKLYN NaN Precinct Closed 10/31/2013 09:34:41 AM 10/31/2013 01:48:26 AM 09 BROOKLYN BROOKLYN 997941.0 182725.0 NaN BROOKLYN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.668204 -73.950648 (40.66820406598287, -73.95064760056546)

Problem 5.

data[data['City']=='NEW YORK'].shape[0]
22664

Problem 6.

data1 = data['City'].str.contains('NEW YORK').fillna(False)
data['City'][data1].unique()
array(['NEW YORK', 'WEST NEW YORK', 'NEW YORK CITY'], dtype=object)

Problem 7.

data_nypd = data[data['Agency'] == "NYPD"]
data_nypd = data_nypd[data_nypd['Closed Date'].notnull()]
data_nypd
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 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
2 26594139 10/31/2013 02:00:24 AM 10/31/2013 02:40:32 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 10032 4060 BROADWAY BROADWAY WEST 171 STREET WEST 172 STREET NaN NaN ADDRESS NEW YORK NaN Precinct Closed 10/31/2013 10:00:24 AM 10/31/2013 02:39:42 AM 12 MANHATTAN MANHATTAN 1001088.0 246531.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.843330 -73.939144 (40.84332975466513, -73.93914371913482)
3 26595721 10/31/2013 01:56:23 AM 10/31/2013 02:21:48 AM NYPD New York City Police Department Noise - Vehicle Car/Truck Horn Street/Sidewalk 10023 WEST 72 STREET WEST 72 STREET COLUMBUS AVENUE AMSTERDAM AVENUE NaN NaN BLOCKFACE NEW YORK NaN Precinct Closed 10/31/2013 09:56:23 AM 10/31/2013 02:21:10 AM 07 MANHATTAN MANHATTAN 989730.0 222727.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.778009 -73.980213 (40.7780087446372, -73.98021349023975)
7 26595195 10/31/2013 01:44:19 AM 10/31/2013 01:58:49 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11417 137-09 CROSSBAY BOULEVARD CROSSBAY BOULEVARD PITKIN AVENUE VAN WICKLEN ROAD NaN NaN ADDRESS OZONE PARK NaN Precinct Closed 10/31/2013 09:44:19 AM 10/31/2013 01:58:49 AM 10 QUEENS QUEENS 1027776.0 184076.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.671816 -73.843092 (40.67181584567338, -73.84309181950769)
8 26590540 10/31/2013 01:44:14 AM 10/31/2013 02:28:04 AM NYPD New York City Police Department Noise - Commercial Loud Talking Club/Bar/Restaurant 10011 258 WEST 15 STREET WEST 15 STREET 7 AVENUE 8 AVENUE NaN NaN ADDRESS NEW YORK NaN Precinct Closed 10/31/2013 09:44:14 AM 10/31/2013 02:00:56 AM 04 MANHATTAN MANHATTAN 984031.0 208847.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.739913 -74.000790 (40.73991339303542, -74.00079028612932)
9 26594392 10/31/2013 01:34:41 AM 10/31/2013 02:23:51 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11225 835 NOSTRAND AVENUE NOSTRAND AVENUE UNION STREET PRESIDENT STREET NaN NaN ADDRESS BROOKLYN NaN Precinct Closed 10/31/2013 09:34:41 AM 10/31/2013 01:48:26 AM 09 BROOKLYN BROOKLYN 997941.0 182725.0 NaN BROOKLYN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.668204 -73.950648 (40.66820406598287, -73.95064760056546)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
111061 26426164 10/04/2013 12:03:00 AM 10/04/2013 02:14:57 AM NYPD New York City Police Department Noise - Commercial Loud Music/Party Club/Bar/Restaurant 11106 30-09 BROADWAY BROADWAY 30 STREET 31 STREET NaN NaN ADDRESS ASTORIA NaN Precinct Closed 10/04/2013 08:03:00 AM 10/04/2013 02:14:57 AM 01 QUEENS QUEENS 1004746.0 217004.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.762279 -73.926013 (40.762278541098084, -73.92601303458156)
111065 26428083 10/04/2013 12:01:05 AM 10/04/2013 02:13:50 AM NYPD New York City Police Department Illegal Parking Posted Parking Sign Violation Street/Sidewalk 11434 NaN NaN NaN NaN GUY R BREWER BOULEVARD ROCKAWAY BOULEVARD INTERSECTION JAMAICA NaN Precinct Closed 10/04/2013 08:01:05 AM 10/04/2013 02:13:50 AM 13 QUEENS QUEENS 1048801.0 178419.0 NaN QUEENS NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.656160 -73.767353 (40.656160351546845, -73.76735262738222)
111066 26428987 10/04/2013 12:00:45 AM 10/04/2013 01:25:01 AM NYPD New York City Police Department Noise - Street/Sidewalk Loud Talking Street/Sidewalk 10016 344 EAST 28 STREET EAST 28 STREET MOUNT CARMEL PLACE 1 AVENUE NaN NaN ADDRESS NEW YORK NaN Precinct Closed 10/04/2013 08:00:45 AM 10/04/2013 01:25:01 AM 06 MANHATTAN MANHATTAN 990637.0 208987.0 NaN MANHATTAN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.740295 -73.976952 (40.740295354643706, -73.97695165980414)
111067 26426115 10/04/2013 12:00:28 AM 10/04/2013 04:17:32 AM NYPD New York City Police Department Noise - Commercial Loud Talking Club/Bar/Restaurant 11226 1233 FLATBUSH AVENUE FLATBUSH AVENUE AVENUE D NEWKIRK AVENUE NaN NaN ADDRESS BROOKLYN NaN Precinct Closed 10/04/2013 08:00:28 AM 10/04/2013 04:17:32 AM 14 BROOKLYN BROOKLYN 996654.0 172515.0 NaN BROOKLYN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.640182 -73.955306 (40.64018174662485, -73.95530566958138)
111068 26428033 10/04/2013 12:00:10 AM 10/04/2013 01:20:52 AM NYPD New York City Police Department Blocked Driveway Partial Access Street/Sidewalk 11236 1259 EAST 94 STREET EAST 94 STREET AVENUE J AVENUE K NaN NaN ADDRESS BROOKLYN NaN Precinct Closed 10/04/2013 08:00:10 AM 10/04/2013 01:20:52 AM 18 BROOKLYN BROOKLYN 1011804.0 172470.0 NaN BROOKLYN NaN NaN NaN NaN NaN NaN NaN NaN NaN N NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40.640024 -73.900717 (40.640024057399216, -73.90071711703163)

15248 rows × 52 columns

Problem 8.

data_nypd['Location Type'].value_counts()
Street/Sidewalk               11997
Club/Bar/Restaurant            1859
Store/Commercial                833
Park/Playground                 247
Residential Building/House      231
House of Worship                 66
Highway                           9
Residential Building              3
Bridge                            1
Roadway Tunnel                    1
Pier                              1
Name: Location Type, dtype: int64

Problem 9.

data_nypd['Location Type'].value_counts().plot.pie()
<matplotlib.axes._subplots.AxesSubplot at 0x7f5255e72b38>

output_16_1

Problem 10.

data_frame = data[['Closed Date','Agency','Descriptor', 'Location Type']]
data_frame = data_frame.dropna()
data_frame
Closed Date Agency Descriptor Location Type
2 10/31/2013 02:40:32 AM NYPD Loud Music/Party Club/Bar/Restaurant
3 10/31/2013 02:21:48 AM NYPD Car/Truck Horn Street/Sidewalk
7 10/31/2013 01:58:49 AM NYPD Loud Music/Party Club/Bar/Restaurant
8 10/31/2013 02:28:04 AM NYPD Loud Talking Club/Bar/Restaurant
9 10/31/2013 02:23:51 AM NYPD Loud Music/Party Club/Bar/Restaurant
... ... ... ... ...
111064 10/07/2013 04:07:16 PM DPR Structure - Outdoors Park
111065 10/04/2013 02:13:50 AM NYPD Posted Parking Sign Violation Street/Sidewalk
111066 10/04/2013 01:25:01 AM NYPD Loud Talking Street/Sidewalk
111067 10/04/2013 04:17:32 AM NYPD Loud Talking Club/Bar/Restaurant
111068 10/04/2013 01:20:52 AM NYPD Partial Access Street/Sidewalk

38492 rows × 4 columns

Problem 11.

data_frame2 =data_frame[data_frame['Closed Date'].str.contains("PM")]

PM1 = data_frame2[data_frame2['Closed Date'].str.contains(" 01:")]
PM3 = data_frame2[data_frame2['Closed Date'].str.contains(" 03:")]
PM5 = data_frame2[data_frame2['Closed Date'].str.contains(" 05:")]
PM7 = data_frame2[data_frame2['Closed Date'].str.contains(" 07:")]

PM1 = PM1['Agency'].value_counts()
PM3 = PM3['Agency'].value_counts()
PM5 = PM5['Agency'].value_counts()
PM7 = PM7['Agency'].value_counts()

data_frame3 = pd.DataFrame({'01 PM': PM1, '03 PM':PM3, '05 PM': PM5, '07 PM':PM7})
data_frame3 = data_frame3.dropna()
data_frame3
01 PM 03 PM 05 PM 07 PM
DOT 188.0 396 82.0 2.0
DPR 190.0 122 40.0 5.0
DSNY 64.0 85 45.0 35.0
FDNY 38.0 66 58.0 51.0
HRA 27.0 30 20.0 23.0
NYPD 521.0 956 448.0 561.0

Problem 12.

data_frame_pm3 = data_frame3.sort_values(by = '03 PM').plot(kind = 'area', stacked=True)
data_frame_pm3
<matplotlib.axes._subplots.AxesSubplot at 0x7f5255e395c0>

output_22_1

카테고리:

업데이트:

댓글남기기