Last active
March 26, 2020 16:56
-
-
Save lossyrob/35e8ce8c91e18d79ac27404ad7525130 to your computer and use it in GitHub Desktop.
New confirmed COVID-19 cases per county over time
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
from datetime import datetime | |
import pandas as pd | |
import requests | |
def get_usafacts_county_cases(url): | |
r = requests.get(url) | |
return pd.read_csv(io.BytesIO(r.content)) | |
def get_new_cases(url): | |
def isdate(x): | |
try: | |
datetime.strptime(x, '%m/%d/%Y') | |
return True | |
except: | |
return False | |
df = get_usafacts_county_cases(url) | |
date_columns = list(filter(lambda x: isdate(x), df.columns)) | |
total_cases = df.set_index('countyFIPS')[date_columns].copy() | |
x = pd.concat([total_cases, -1 * total_cases.shift(1, axis=1)]) | |
new_cases = x.reset_index().groupby('countyFIPS').sum().reset_index() | |
df = df.drop(columns=date_columns).merge(new_cases, on='countyFIPS') | |
df = df[df['countyFIPS'] != 0] | |
return df | |
new_confirmed_cases_per_county = get_new_cases( | |
'https://static.usafacts.org/public/data/covid-19/covid_confirmed_usafacts.csv', | |
) | |
new_confirmed_cases_per_county |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment