Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
scraper for count of Swiss coronavirus confirmed cases
#!/usr/bin/python3
# pip3 install pandas matplotlib
import pandas as pd
from matplotlib import pyplot as plt
# this gets the worldwide confirmed cases from JHU
url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv'
# read/download csv
df = pd.read_csv(url)
# filter on Swiss data
df = df[df['Country/Region'] == 'Switzerland']
# remove unnecessary columns
df.drop(['Province/State', 'Lat','Long'], axis=1, inplace=True)
# unpivot aka "melt"
df = df.melt(id_vars=['Country/Region'], var_name='date', value_name='confirmed_cases')
# rename column
df.rename(columns={'Country/Region': 'country'}, inplace=True)
# convert date column to date-type
df.date = pd.to_datetime(df.date)
# not needed
#df = df.set_index(df.date)
df.to_csv('confirmed_coronavirus.csv',index=False)
#print(df)
# write to plot, save as file
plt.plot(df.date, df.confirmed_cases, '-')
_ = plt.xticks(rotation=45)
plt.savefig('confirmed_coronavirus.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.