Py.Cafe

jragh./

plotly-motor-vehicle-collisions-analysis

Motor Vehicle Collisions Analysis

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# check out https://dash.plotly.com/ for documentation
# And check out https://py.cafe/maartenbreddels for more examples
from dash import Dash, Input, Output, callback, dcc, html
import pandas as pd
import plotly.express as px

## Variable pointing to github repo with raw CSV data ##
raw_github_url = "https://raw.githubusercontent.com/jragh/plotlymeetup/refs/heads/main/July_2025/Motor_Vehicle_Collisions_with_KSI_Data.csv"

## Dataframe creation from CSV url ##
df = pd.read_csv(raw_github_url)

## Dataframe Cleaning: Replacing None Values ##
df_cleaned = df.dropna(subset=["ACCNUM"])

df_cleaned = df_cleaned.loc[df_cleaned['ACCIDENT_YEAR'] >= 2020].copy().reset_index()

## Quick Group by for our first chart ##
## Also adding in the color based on the accident class ##
fig = px.bar(
    df_cleaned.groupby(["ACCIDENT_YEAR", "ACCLASS"])["ACCNUM"].count().reset_index(),
    x="ACCIDENT_YEAR",
    y="ACCNUM",
    color="ACCLASS",
    barmode="stack"
)

## Server ##
app = Dash(__name__)

## Updating the layout for the served html ##
app.layout = html.Div(
    children=[
        
        dcc.Graph(figure=fig)

    ]
)