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)
]