from dash import Dash, dcc, html
import plotly.express as px
import pandas as pd
# Load the dataset
file_path = "https://raw.githubusercontent.com/Coding-with-Adam/Dash-by-Plotly/refs/heads/master/Other/NYC%20Marathon%20Results%2C%202024%20-%20Marathon%20Runner%20Results.csv"
df = pd.read_csv(file_path)
# Convert overallTime to seconds for easier calculations
def time_to_seconds(time_str):
if isinstance(time_str, str):
h, m, s = map(int, time_str.split(":"))
return h * 3600 + m * 60 + s
return None
df["overallTime_seconds"] = df["overallTime"].apply(time_to_seconds)
# Best Age Group for Running Performance
age_group_performance = df.groupby("age")["overallTime_seconds"].mean().reset_index()
# Age vs. Finishing Time Correlation
fig_age_vs_time = px.scatter(
df, x="age", y="overallTime_seconds",
title="Age vs. Finishing Time Correlation",
labels={"age": "Age", "overallTime_seconds": "Overall Time (Seconds)"},
trendline="ols"
)
# Filter data for US participants
us_runners = df[(df["countryCode"].astype(str) == "USA") & (df["stateProvince"].astype(str).str.len() == 2)].copy()
# Group by state and calculate average finishing time
state_avg_time = us_runners.groupby("stateProvince")["overallTime_seconds"].mean().reset_index()
# Convert back to HH:MM:SS format
state_avg_time["avg_overallTime"] = state_avg_time["overallTime_seconds"].apply(
lambda x: f"{int(x//3600)}:{int((x%3600)//60):02}:{int(x%60):02}"
)
# Create a Plotly figure
fig_state = px.bar(
state_avg_time.sort_values("overallTime_seconds"),
x="stateProvince",
y="overallTime_seconds",
text="avg_overallTime",
title="Average Finishing Time by State (US Runners)",
labels={"stateProvince": "State", "overallTime_seconds": "Average Finishing Time (Seconds)"},
)
fig_state.update_traces(textposition="outside")
fig_state.update_layout(xaxis_title="State", yaxis_title="Average Finishing Time (Seconds)", xaxis={'categoryorder':'total ascending'})
# Dash app setup
app = Dash(__name__)
app.layout = html.Div(
children=[
dcc.Graph(figure=fig_state),
dcc.Graph(figure=fig_age_vs_time)
]
)
if __name__ == "__main__":
app.run_server(debug=True)