<jupyter_start><jupyter_text>Identifying High Risk Areas from NYC Traffic Conditions*Data Cleaning -*<jupyter_code># libraries

# pliers
import pandas as pd
import numpy as np
import missingno as msno

# GIS 
import geopandas as gpd
from shapely.geometry import Point, Polygon

# viz
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Data Ingestion<jupyter_code>df = pd.read_csv('../data/crash_features_2019.csv')
print(df.shape)
df.head()<jupyter_output>(193813, 30)<jupyter_text>Create Day of Week Column<jupyter_code># Checking date type

type(df['DATE'][1])
# creates datatime for date column

df['DATE'] = pd.to_datetime(df['DATE'])
# creates a columns 

df['dayofweek'] =  df['DATE'].dt.dayofweek
# convert dayofweek to char 

df['dayofweek'] = df['dayofweek'].map ({0:'Mon',1:'Tues',2:'Wed',3:'Thurs',4:'Fri', 5:'Sat',6:'Sun' })
df.iloc[:,-1]<jupyter_output><empty_output><jupyter_text>Highways / Expressways / Parkways Locating the highways, and creating a boolian column .<jupyter_code># randomly looking through street names to discover highway names

df.iloc[:,8:11].sample(10)
# mask for highways 

highways = "EXPRESSWAY|PKWY|PARKWAY|EXPY|FDR|BQE|EXPWY|HIGHWAY|HENRY HUDSON"
# number of rows where the crash was located on the a 'highway '

df[df['ON STREET NAME'].str.contains(highways, na =False)]['ON STREET NAME'].count()
# a list of the the unique highway names 

np.unique(df[df['ON STREET NAME'].str.contains(highways, na =False)]['ON STREET NAME'])<jupyter_output><empty_output><jupyter_text>Create a Boolian Column, True crash was on a highway<jupyter_code>#create a Boolian Column, True crash was on a highway 

df['highway'] =  df['ON STREET NAME'].str.contains(highways, na =False)
df['highway'].sample<jupyter_output><empty_output><jupyter_text>a look<jupyter_code>df.head()
# convert to np.... 
    
    # check which rows are missing zip code and borough 
    if (pd.isnull(row['ZIP CODE']) or pd.isnull(row['BOROUGH'])):# and (row['LOCATION']):
        
        # create Point for geo-positioning 
        geo_point = Point(row['LONGITUDE'],row['LATITUDE'])

        # itterates through and pulls zipcode polys to check 
        for ii, zipcode in zipcodes.iterrows():
            
            # checks polys and updates df rows
            if zipcode['geometry'].contains(geo_point):
                row['ZIP CODE'] = zipcode['postalCode']
                row['BOROUGH'] = zipcode['borough']
                df.at[i,'ZIP CODE'] = zipcode['postalCode']
                df.at[i,'BOROUGH'] = zipcode['borough'].upper()
                c += 1
                break

print(f'Number of rows updated {c}')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup