Author: Ken Blake, Ph.D.

Description: This script will read the latest data from the New York Times' county-level COVID-19 database at https://github.com/nytimes/covid-19-data/blob/master/us-counties.csv, filter the data for a chosen county in a chosen state, calculate the daily count of new cases and new deaths, print the most recent 28 days' worth of data for the selected county, and save the county's data for all dates to a comma-separated value (.csv) file on your computer. The printed data can be copied and pasted directly into a spreadsheet for further analysis and visualization.

Note: For information about the data, see https://github.com/nytimes/covid-19-data.

Note: After you have run the script one time in a given Anaconda environment, you may reduce the script's execution time by adding a # in front of pip install pandas. For example, #pip install pandas instead of pip install pandas. The # will tell Python to skip the code without running it.

In [ ]:
pip install pandas
In [ ]:
import pandas as pd
countydata = pd.read_csv('https://github.com/nytimes/covid-19-data/raw/master/us-counties.csv')

Directions: In the code below, replace Tennessee with the state of your choice, and replace Rutherford with the county of your choice. Note that both must be capitalized, and the county you choose must be in the state that you choose.

In [ ]:
localdata = countydata.query('state=="Tennessee" & county=="Rutherford"')
In [ ]:
localdata.to_csv(r'NYTCovidData.csv',index = False)
localdata = pd.read_csv('NYTCovidData.csv')
localdata['New cases'] = localdata['cases'] - localdata['cases'].shift(1).fillna(0)
localdata['New deaths'] = localdata['deaths'] - localdata['deaths'].shift(1).fillna(0)
localdata = localdata.drop([
    "fips"],
    axis=1)
In [ ]:
localdata.tail(28)

Directions: The code below will save the selected county's data in a file named NYTCovidData.csv. The file will be saved to whatever directory you have saved your Jupyter Notebook in. By default, each execution of this script will overwrite any already-existing NYTCovidData.csv in the directory. To prevent the overwrite, change NYTCovidData.csv to a different file name before running the script.

Note: The script will produce an error if the NYTCovidData.csv is open in an application at the time you run the script. To avoid the error, close the file or save it under a different file name.

In [ ]:
localdata.to_csv(r'NYTCovidData.csv',index = False)