Created
July 26, 2020 18:46
-
-
Save sampathweb/3c6cc70268d5997296d7cf8c491a285d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import seaborn as sns | |
import streamlit as st | |
# Youtube Tutorial | |
# https://www.youtube.com/channel/UC3LD42rjj-Owtxsa6PwGU5Q | |
@st.cache | |
def load_data(data_path): | |
df = pd.read_csv(data_path) | |
df["date"] = pd.to_datetime(df["date"]) | |
df.set_index("date", inplace=True) | |
df["cases"] = df.groupby("location")["new_cases"].transform( | |
lambda x: x.rolling(7).mean() | |
) | |
df["deaths"] = df.groupby("location")["new_deaths"].transform( | |
lambda x: x.rolling(7).mean() | |
) | |
return df | |
df = load_data("covid_world_data.csv") | |
"## Display Data" | |
df | |
df_country_tot = df.groupby("location")[["new_cases", "new_deaths"]].sum() | |
df_country_tot["mortality_ratio"] = df_country_tot["new_deaths"] / ( | |
df_country_tot["new_deaths"] + df_country_tot["new_cases"] | |
) | |
"## Top 10 Countries by Mortality Ratio" | |
st.write(df_country_tot.sort_values("mortality_ratio", ascending=False).head(10)) | |
"## Top 10 Countries by cases" | |
st.write(df_country_tot.sort_values("new_cases", ascending=False).head(10)) | |
country = st.sidebar.selectbox("Country: ", df["location"].unique()) | |
st.write(f"### {country} Trend") | |
df[df["location"] == country][["cases", "deaths"]].plot() | |
st.pyplot() | |
countries = st.multiselect("Select countries", df["location"].unique()) | |
choice_col = st.radio("Compare: ", ("deaths", "cases")) | |
df[df["location"].isin(countries)].groupby("location")[choice_col].plot(legend=True) | |
st.pyplot() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment