Py.Cafe

antfperez/

dash-squirrel-census-insights

Squirrel Census Insights

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
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
67
68
69
70
71
# check out https://dash.plotly.com/ for Dash documentation
# check out https://plotly.com/python for Plotly documentation
# check out Plotly High Level refernece docs: https://plotly.com/python-api-reference/plotly.express.html

# Plotly Studio sign-up: https://docs.google.com/spreadsheets/d/1Z4DVKKXIeFOpe6mOfBGN5QAdYeJrW4X2mKT1UlxeYDE/edit?usp=sharing
# Adam's LinkedIn: https://www.linkedin.com/in/charming-data/

from dash import Dash, Input, Output, callback, dcc, html
import pandas as pd
import plotly.express as px

# Download data (Github): https://github.com/plotly/datasets/blob/master/2018_Central_Park_Squirrel_Census_-_Squirrel_Data_20250721.csv
# Downlaod data (NYC portal): https://data.cityofnewyork.us/Environment/2018-Central-Park-Squirrel-Census-Squirrel-Data/vfnx-vebw/about_data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/2018_Central_Park_Squirrel_Census_-_Squirrel_Data_20250721.csv')

"""
Use this sectiont to slice, clean, filter data to prepare it for the figure
"""
#daily_counts = df.groupby("Shift").size().reset_index(name="Count")

df['ISODate'] = pd.to_datetime(df['Date'].astype('str'), format='%m%d%Y')
#print(sorted(pd.to_datetime(df['ISODate'].unique())))
daily_counts = df.groupby("ISODate").size().reset_index(name="Count")

#print(sorted(pd.to_datetime(df['ISODate'].unique())))
df['DayOfWeekNum'] = pd.to_datetime(df['ISODate']).dt.weekday
print(df[['ISODate', 'DayOfWeekNum']].head())

dow_counts = df.groupby("DayOfWeekNum").size().reset_index(name="Count")

# Count per date
counts_by_date = df.groupby('ISODate').size().reset_index(name='Count')

# Extract day of week as number (0=Monday)
counts_by_date['DayOfWeekNum'] = counts_by_date['ISODate'].dt.weekday

# Average count per weekday
dow_avg_counts = counts_by_date.groupby('DayOfWeekNum')['Count'].mean().reset_index()
print(dow_avg_counts)



# Build the figure
fig = px.bar(
    daily_counts,
    x="ISODate",
    y="Count",
    title="",
    labels={"Count": "Number of Sightings", "Date": "Date"}
)

# Build the figure
fig2 = px.bar(
    dow_avg_counts,
    x="DayOfWeekNum",
    y="Count",
    title="",
    labels={"Count": "Number of Sightings", "Date": "Date"}
)

app = Dash(__name__)
app.layout = html.Div(
   children=[
        # assign the fig to the 'figure' property to view
        dcc.Graph(figure=fig),
        dcc.Graph(figure=fig2)
    ]
)

# Plotly Studio sample -- https://centralparksquirrels.plotly.app