Py.Cafe

adamschroeder.m/

June-NYC-Plotly-Meetups

Violations Count Analysis

DocsPricing
  • Open_Parking_and_Camera_Violations_sample.csv
  • 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from dash import Dash, dcc   # https://dash.plotly.com/
import dash_ag_grid as dag
import plotly.express as px  # https://plotly.com/python/
import pandas as pd

# Download of complete CSV sheet from Google Drive: https://drive.google.com/file/d/1ZdH5C3kWaiRR3fOotvdW8l351YrKQJgG/view?usp=sharing
# This data app only uses 200 rows of the data becuase py.cafe limits data file size
df = pd.read_csv("Open_Parking_and_Camera_Violations_sample.csv")

# filter only for NY state
# df = df[df['State'] == 'NY']
# fig = px.histogram(df, x='License Type')

df['Issue Date'] = pd.to_datetime(df['Issue Date'])
df['Day of Week'] = df['Issue Date'].dt.day_name()

# Filter the DataFrame to include only the top 7 most common agencies.
top_7_agencies = df['Issuing Agency'].value_counts().nlargest(7).index.tolist()
df_filtered = df[df['Issuing Agency'].isin(top_7_agencies)].copy()

# Group by 'Day of Week' and 'Issuing Agency' and count the number of violations in each group.
violations_summary = df_filtered.groupby(['Day of Week', 'Issuing Agency']).size().reset_index(name='Count')

# Prep for plotting
days_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
violations_summary['Day of Week'] = pd.Categorical(
    violations_summary['Day of Week'],
    categories=days_order,
    ordered=True
)
violations_summary = violations_summary.sort_values('Day of Week')

fig = px.bar(
    violations_summary,
    x='Day of Week',
    y='Count',
    color='Issuing Agency',
    barmode='stack',
    title='Count of Violations by Issuing Agency and Day of Week',
    labels={
        'Count': 'Number of Violations',
        'Day of Week': 'Day of the Week',
        'Issuing Agency': 'Issuing Agency'
    },
    template='plotly_white'
)

fig.update_layout(
    xaxis_title="Day of the Week",
    yaxis_title="Number of Violations",
    legend_title="Issuing Agency"
)


grid = dag.AgGrid(
    rowData=df.to_dict("records"),
    columnDefs=[{"field": i, 'filter': True, 'sortable': True} for i in df.columns],
    dashGridOptions={"pagination": True},
    # columnSize="sizeToFit"
)

app = Dash()
app.layout = [
    grid,
    dcc.Graph(figure=fig)
]