|  | 
|  | 1 | +import streamlit as st | 
|  | 2 | +import pandas as pd | 
|  | 3 | +import numpy as np | 
|  | 4 | +import pydeck as pdk | 
|  | 5 | +import plotly.express as px | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +DATA_URL = ( | 
|  | 9 | + "C:/Users/Atharva/Desktop/Project/project 2/streamlit-nyc/Motor_Vehicle_Collisions_-_Crashes.csv" | 
|  | 10 | +) | 
|  | 11 | + | 
|  | 12 | +st.title("Motor Vehicle Collisions in New York City") | 
|  | 13 | +st.markdown("This application s a streamlit dashboard that can be used to analyze motor Vehicle Collisions in NYC 🗽") | 
|  | 14 | + | 
|  | 15 | +@st.cache(persist=True) | 
|  | 16 | +def load_data(nrows): | 
|  | 17 | + data = pd.read_csv(DATA_URL, nrows=nrows, parse_dates= [['CRASH_DATE', 'CRASH_TIME']]) | 
|  | 18 | + data.dropna(subset=['LATITUDE','LONGITUDE'], inplace=True) | 
|  | 19 | + lowercase = lambda x: str(x).lower() | 
|  | 20 | + data.rename(lowercase, axis='columns', inplace=True) | 
|  | 21 | + data.rename(columns={'crash_date_crash_time': 'date/time'}, inplace=True) | 
|  | 22 | + return data | 
|  | 23 | + | 
|  | 24 | +data = load_data(100000) | 
|  | 25 | +original_data = data | 
|  | 26 | + | 
|  | 27 | + | 
|  | 28 | +st.header("Where are most people injured in nyc") | 
|  | 29 | +injured_people = st.slider("Number of persons injured in vehicle collisions", 0,19) | 
|  | 30 | +st.map(data.query("injured_persons >= @injured_people")[["latitude", "longitude"]].dropna(how="any")) | 
|  | 31 | + | 
|  | 32 | + | 
|  | 33 | +st.header("How many collisions occur during a given time of day?") | 
|  | 34 | +hour = st.slider("Hour to look at", 0,23) | 
|  | 35 | +data = data[data['date/time'].dt.hour == hour] | 
|  | 36 | + | 
|  | 37 | +st.markdown("Vehicle collision between %i:00 and %i:00" % (hour,(hour + 1)%24)) | 
|  | 38 | +midpoint = (np.average(data['latitude']), np.average(data['longitude'])) | 
|  | 39 | + | 
|  | 40 | +st.write(pdk.Deck( | 
|  | 41 | + map_style="mapbox://styles/mapbox/light-v9", | 
|  | 42 | + initial_view_state={ | 
|  | 43 | + "latitude": midpoint[0], | 
|  | 44 | + "longitude": midpoint[1], | 
|  | 45 | + "zoom": 11, | 
|  | 46 | + "pitch": 50, | 
|  | 47 | + }, | 
|  | 48 | + layers = [ | 
|  | 49 | + pdk.Layer( | 
|  | 50 | + "HexagonLayer", | 
|  | 51 | + data= data[['date/time', 'latitude', 'longitude']], | 
|  | 52 | + get_position=['longitude', 'latitude'], | 
|  | 53 | + radius=100, #radius of hexagon | 
|  | 54 | + extruded=True, #3-d visulations | 
|  | 55 | + pickable=True, | 
|  | 56 | + elevation_range=[0,1000], | 
|  | 57 | + ), | 
|  | 58 | + ] | 
|  | 59 | +)) | 
|  | 60 | + | 
|  | 61 | +st.subheader("Breakdown by minute between %i:00 and %i:00" % (hour,(hour+1))) | 
|  | 62 | +filtered = data[ | 
|  | 63 | + (data['date/time'].dt.hour >= hour ) & (data['date/time'].dt.hour <( hour+1 )) | 
|  | 64 | +] | 
|  | 65 | +hist = np.histogram(filtered['date/time'].dt.minute, bins=60, range= (0,60))[0] | 
|  | 66 | +chart_data = pd.DataFrame({'minute': range(60), 'crashes':hist}) | 
|  | 67 | +fig = px.bar(chart_data, x='minute', y='crashes', hover_data=['minute', 'crashes'], height=400) | 
|  | 68 | +st.write(fig) | 
|  | 69 | + | 
|  | 70 | +st.header("Top 5 dangerous streets by affected type") | 
|  | 71 | +select=st.selectbox('Affected type of people', ['Pedestrians', 'Cyclists', 'Motorists']) | 
|  | 72 | + | 
|  | 73 | +if select == 'Pedestrains': | 
|  | 74 | + st.write(original_data.query("injured_pedstrains >=1")[["on_street_name", "injured_pedstrains"]].sort_values(by=['injured_pedstrains'], ascending=False).dropna(how='any')[:5]) | 
|  | 75 | + | 
|  | 76 | +elif select == 'Cyclists': | 
|  | 77 | + st.write(original_data.query("injured_cyclists >=1")[["on_street_name", "injured_cyclists"]].sort_values(by=['injured_cyclistss'], ascending=False).dropna(how='any')[:5]) | 
|  | 78 | + | 
|  | 79 | +else: | 
|  | 80 | + st.write(original_data.query("injured_motorists >=1")[["on_street_name", "injured_motorists"]].sort_values(by=['injured_motorists'], ascending=False).dropna(how='any')[:5]) | 
|  | 81 | + | 
|  | 82 | + | 
|  | 83 | +if st.checkbox("Show Raw Data", False): | 
|  | 84 | +st.subheader('Raw Data') | 
|  | 85 | +st.write(data) | 
0 commit comments