Thursday, 29 September 2016

NYPD Vehicle Collisions Using python


The data link is here

import pandas as pd

csv_path = 'https://raw.githubusercontent.com/nygeog/data/master/nyc_crashes/data/NYPD_Motor_Vehicle_Collisions.csv'
inCSV = 'data/NYPD_Motor_Vehicle_Collisions.csv'
ouCSV = 'data/nypd_mv_collisions.csv'

df = pd.read_csv(inCSV).rename(columns=lambda x: x.lower())

#drop ones w/out valid lat #super lazy, just grabbing lat's above 35
df = df[(df.latitude > 35)]
#print df.dtypes 
print len(df.index)
df.head(5)

The original CSV data (first 5 records)

datetimeboroughzip codelatitudelongitudelocationon street namecross street nameoff street name...contributing factor vehicle 2contributing factor vehicle 3contributing factor vehicle 4contributing factor vehicle 5unique keyvehicle type code 1vehicle type code 2vehicle type code 3vehicle type code 4vehicle type code 5
102/13/201521:45MANHATTAN1000240.715622-73.994275(40.7156221, -73.9942752)FORSYTH STREETCANAL STREETNaN...NaNNaNNaNNaN3168577PASSENGER VEHICLEUNKNOWNNaNNaNNaN
202/13/201521:45MANHATTAN1000140.747535-73.988307(40.7475349, -73.9883068)WEST 31 STREETBROADWAYNaN...Fatigued/DrowsyNaNNaNNaN3169163TAXITAXINaNNaNNaN
302/13/201521:45BRONX1046240.833558-73.857732(40.8335582, -73.8577325)WESTCHESTER AVENUEPUGSLEY AVENUENaN...UnspecifiedNaNNaNNaN3169251PASSENGER VEHICLESPORT UTILITY / STATION WAGONNaNNaNNaN
402/13/201521:44MANHATTAN1001740.748800-73.969846(40.7487997, -73.969846)EAST 42 STREET1 AVENUENaN...Other VehicularNaNNaNNaN3169176PASSENGER VEHICLEPASSENGER VEHICLENaNNaNNaN
502/13/201521:40STATEN ISLAND1030440.617295-74.080479(40.6172954, -74.0804791)PARK HILL AVENUEOSGOOD AVENUENaN...UnspecifiedNaNNaNNaN3169614PASSENGER VEHICLEPASSENGER VEHICLENaNNaNNaN
5 rows × 29 columns



Mergin' the date (day) and time (hours)

#create datetime http://stackoverflow.com/questions/17978092/combine-date-and-time-columns-using-python-pandas
df['datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'])


df = df[['datetime','latitude','longitude']]
df = df[(df.datetime > '2014-01-01 00:00:01')] #query out only data from 2014 onward
df = df.sort('datetime')
df.to_csv(ouCSV,index=False)
print len(df.index)

192324 #count of records 2014+

df.head(5)

Clean and small data to import to CartoDB

datetimelatitudelongitude
2288542014-01-01 00:01:0040.725432-73.996771
2288502014-01-01 00:01:0040.767889-73.981512
2288552014-01-01 00:01:0040.750844-73.978608
2291442014-01-01 00:15:0040.588646-73.992452
2288492014-01-01 00:20:0040.689019-73.986157



Maps in CartoDB

PostGIS SQL Statement to grab 2014 from the 2014-2015/02/13 Table

SELECT * FROM nypd_mv_collisions_2014_20150213 WHERE datetime BETWEEN '2014-01-01 00:00:00' and '2015-01-01 00:00:00'

Density Hex Bins of 2014 Collisions

I'm not totally sure if when creating the classes if it ignores hex bins with no collisions or what. Need to look into that.


Density of 2014 Collisions over time (using Torque)

Animation of Density of Collisions. I think these are Kernel Densities but not sure, need to check CartoDB documentation.



Valentine's Day 2014 Collisions (Clickbait)















1 comment: