Spaces:
Sleeping
Sleeping
File size: 4,465 Bytes
6f00d18 |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import json
import datetime
st.set_page_config(layout="wide")
st.header("Covid Dashboard")
@st.cache_data
def load_covid_data(fp):
print('Running load_covid_data...')
# read in the csv via the link
df = pd.read_csv(fp)
# Creating the National Average
national_average = df.groupby('date')['cases_avg_per_100k'].mean()
# turn that into a data frame
national_average = pd.DataFrame(national_average).reset_index()
# create a state column
national_average['state'] = 'National Average'
# add it to the main dataframe
df = pd.concat([df, national_average])
df['datetime'] = pd.to_datetime(df['date'])
return(df)
# loading the data
fp = 'https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us-states.csv'
df = load_covid_data(fp)
# -- Create two columns
col1, col2 = st.columns( [1,4] )
with col1:
st.write('Average number of cases per 100k compared to the national average.')
with col2:
# create a list of all the state names
state_list = sorted(df['state'].unique())
# create a mulit select button
selected_states = st.multiselect(
'Select which states to compare.',
state_list,
default=['National Average']
)
# for debugging
print(type(selected_states), selected_states)
# extract just the selected states
state_df = df[df['state'].isin(selected_states)].copy()
# create summary stats from selected_states dataframe
summary_stats = state_df.groupby('state')['cases_avg_per_100k'].agg(['mean', 'median', 'min', 'max', 'std'])
# This should work but doesn't
summary_stats.index = summary_stats.index.rename('Avg Cases Per 100k')
# display the summary stats table
st.write('Avg Cases Per 100k')
st.dataframe(summary_stats)
# create line chart with just selected states
fig = px.line(state_df,
x='date',
y='cases_avg_per_100k',
line_group='state',
color='state',
title="Average # of cases per 100k people.",
labels={ "cases_avg_per_100k": "Cases per 100k"}
)
# changes the background color
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
})
# changing the grid axes
fig.update_xaxes(showgrid=False, gridwidth=1, gridcolor='Gray')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='Gray')
# display graph
st.plotly_chart(fig, use_container_width=True)
# create line chart of just last 90 days.
today = state_df['datetime'].max()
d = datetime.timedelta(days = 90)
last_90 = today - d
df_last_ninety_days = state_df[state_df['datetime'] > last_90]
fig = px.line(df_last_ninety_days,
x='date',
y='cases_avg_per_100k',
line_group='state',
color='state',
title="Last 90 Days of data: Average # of cases per 100k people.",
labels={ "cases_avg_per_100k": "Cases per 100k"}
)
# changest the background color
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
})
# changing the grid axes
fig.update_xaxes(showgrid=False, gridwidth=1, gridcolor='Gray')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='Gray')
# display the chart.
st.plotly_chart(fig, use_container_width=True)
# inside column 1
with col1:
# for each state in selected states
for state in selected_states:
if state != 'National Average':
avg = df[df['state'] == state]['cases_avg_per_100k'].mean()
avg = round(avg, 2)
national_average = df[df['state'] == 'National Average']['cases_avg_per_100k'].mean()
national_average = round(national_average, 2)
delta = avg-national_average
delta = round(delta, 2)
st.metric(label=state, value=avg, delta=delta, help='Average Cases Per 100k and the little number in green or red below is how it compares to the national average')
else:
national_average = df[df['state'] == 'National Average']['cases_avg_per_100k'].mean()
national_average = round(national_average, 2)
st.metric(label=state, value=national_average) #, delta=0.0, delta_color='off')
|