<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Loading the Open COVID-19 Dataset
This very short notebook showcases how to load the Open COVID-19 datset, including some examples for commonly performed operations.
First, loading the data is very simple with pandas. We can use the CSV master table to download the entire Open COVID-19 dataset in a single step
Step1:  Looking at country-level data
Some records contain country-level data, in other words, data that is aggregated at the country level. Other records contain region-level data, which are subdivisions of a country; for example, Chinese provinces or USA states. A few regions also report at an even smaller subdivision, i.e. county/municipality level.
To filter only country-level data from the dataset, look for records that have a aggregation_level == 0 or, alternatively, null value for the subregion1_code (or subregion1_name) field
Step2:  Looking at state/province data
Conversely, to filter state/province data for a specific country, we need to look for records where the aggregation level is 1 (or where the region columns have non-null values). The following snippet extracts data related to Spain's subregions from the dataset
Step3:  Using the key column
The key column is present in all datasets and is unique for each combination of country, province/state and municipality/county. This way, we can retrieve a specific country or region using a single filter for the data. The key column is built using country_code for country-level data, ${country_code}_${subregion1_code} for province/state level data, and ${country_code}_${subregion1_code}_${subregion2_code} for municipality/county data
Step4:  Dataset Subsets
The master table can be large and cumbersome depending on your application. If you only need a subset of the data, you can consult each table individually. For a list of all the available tables, see the README of the repo. For example, here's how you would get only epidemiology data for Madrid
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
# Load CSV data directly from the URL with pandas, the options are needed to prevent
# reading of records with key "NA" (Namibia) as NaN
data = pd.read_csv(
    "https://storage.googleapis.com/covid19-open-data/v3/latest/aggregated.csv",
    keep_default_na=False,
    na_values=[""],
)
# NOTE: We are only downloading the last day of data, but you can download all
# the data at once if you'd like by changing the URL to:
# https://storage.googleapis.com/covid19-open-data/v3/latest/aggregated.csv
# Print a small snippet of the dataset
print(f"The dataset currently contains {len(data)} records, here is a sample:")
data.sample(5)
Explanation: Loading the Open COVID-19 Dataset
This very short notebook showcases how to load the Open COVID-19 datset, including some examples for commonly performed operations.
First, loading the data is very simple with pandas. We can use the CSV master table to download the entire Open COVID-19 dataset in a single step:
End of explanation
# Look for rows with country level data
# Same as `data[data.subregion2_code.isna()]`
countries = data[data.aggregation_level == 0]
# We no longer need the subregion-level columns
countries = countries.drop(columns=['subregion1_code', 'subregion1_name', 'subregion2_code', 'subregion2_name'])
countries.tail()
Explanation: Looking at country-level data
Some records contain country-level data, in other words, data that is aggregated at the country level. Other records contain region-level data, which are subdivisions of a country; for example, Chinese provinces or USA states. A few regions also report at an even smaller subdivision, i.e. county/municipality level.
To filter only country-level data from the dataset, look for records that have a aggregation_level == 0 or, alternatively, null value for the subregion1_code (or subregion1_name) field:
End of explanation
# Filter records that have the right country code AND a non-null region code
# Same as `data[(data.country_code == 'ES') & ~(data.subregion`_code.isna())]`
spain_regions = data[(data.country_code == 'ES') & (data.aggregation_level == 1)]
# We no longer need the municipality-level columns
spain_regions = spain_regions.drop(columns=['subregion2_code', 'subregion2_name'])
spain_regions.tail()
Explanation: Looking at state/province data
Conversely, to filter state/province data for a specific country, we need to look for records where the aggregation level is 1 (or where the region columns have non-null values). The following snippet extracts data related to Spain's subregions from the dataset:
End of explanation
# Filter records for Spain at the country-level
spain_country = data[data.location_key == 'ES']
# We no longer need the subregion-level columns
spain_country = spain_country.drop(columns=['subregion1_code', 'subregion1_name', 'subregion2_code', 'subregion2_name'])
spain_country.tail()
# Filter records for Madrid, one of the subregions of Spain
madrid = data[data.location_key == 'ES_MD']
madrid.tail()
Explanation: Using the key column
The key column is present in all datasets and is unique for each combination of country, province/state and municipality/county. This way, we can retrieve a specific country or region using a single filter for the data. The key column is built using country_code for country-level data, ${country_code}_${subregion1_code} for province/state level data, and ${country_code}_${subregion1_code}_${subregion2_code} for municipality/county data:
End of explanation
# Load the epidemiology table
# Note that all the helper columns such as country code, country name, aggregation level, etc. are present in the
# `index` table; we only have the key here
epi = pd.read_csv('https://storage.googleapis.com/covid19-open-data/v2/epidemiology.csv')
# Filter records for Madrid, one of the subregions of Spain
madrid = epi[epi.location_key == 'ES_MD']
madrid.tail()
Explanation: Dataset Subsets
The master table can be large and cumbersome depending on your application. If you only need a subset of the data, you can consult each table individually. For a list of all the available tables, see the README of the repo. For example, here's how you would get only epidemiology data for Madrid:
End of explanation
<END_TASK>