Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
covid-19-public-data
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christine Choirat
covid-19-public-data
Commits
7d00456b
Commit
7d00456b
authored
5 years ago
by
Chandrasekhar Ramakrishnan
Browse files
Options
Downloads
Patches
Plain Diff
fix: updated names of countries in ToRates notebook
parent
a906d657
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/ToRates.ipynb
+28
-9
28 additions, 9 deletions
notebooks/ToRates.ipynb
with
28 additions
and
9 deletions
notebooks/ToRates.ipynb
+
28
−
9
View file @
7d00456b
...
@@ -132,24 +132,32 @@
...
@@ -132,24 +132,32 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"region_wb_jhu_map = {\n",
"region_wb_jhu_map = {\n",
" 'China': 'Mainland China',\n",
" 'Brunei Darussalam': 'Brunei',\n",
" 'Iran, Islamic Rep.': 'Iran (Islamic Republic of)',\n",
" 'Czech Republic': 'Czechia',\n",
" 'Korea, Rep.': 'Republic of Korea',\n",
" 'United States': 'US',\n",
" 'United Kingdom': 'UK',\n",
" 'Hong Kong SAR, China': 'Hong Kong SAR',\n",
" 'Egypt, Arab Rep.': 'Egypt',\n",
" 'Egypt, Arab Rep.': 'Egypt',\n",
" 'Vietnam': 'Viet Nam',\n",
" 'Hong Kong SAR, China': 'Hong Kong SAR',\n",
" 'Iran, Islamic Rep.': 'Iran',\n",
" 'Korea, Rep.': 'Korea, South',\n",
" 'Macao SAR, China': 'Macao SAR',\n",
" 'Macao SAR, China': 'Macao SAR',\n",
" 'Russian Federation': 'Russia',\n",
" 'Slovak Republic': 'Slovakia',\n",
" 'Slovak Republic': 'Slovakia',\n",
" 'Moldova': 'Republic of Moldova',\n",
" 'St. Martin (French part)': 'Saint Martin',\n",
" 'St. Martin (French part)': 'Saint Martin',\n",
" '
Brunei Darussalam': 'Brunei
'\n",
" '
United States': 'US
'\n",
"}\n",
"}\n",
"current_pop_ser = pop_df[['Country Name', '2018']].copy().replace(region_wb_jhu_map).set_index('Country Name')['2018']\n",
"current_pop_ser = pop_df[['Country Name', '2018']].copy().replace(region_wb_jhu_map).set_index('Country Name')['2018']\n",
"data_pop_ser = current_pop_ser[current_pop_ser.index.isin(frames_map['confirmed'].index.levels[0])]"
"data_pop_ser = current_pop_ser[current_pop_ser.index.isin(frames_map['confirmed'].index.levels[0])]"
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use this to find the name in the series\n",
"# current_pop_ser[current_pop_ser.index.str.contains('Czech')]"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
...
@@ -157,6 +165,17 @@
...
@@ -157,6 +165,17 @@
"There are some regions that we cannot resolve, but we will just ignore these."
"There are some regions that we cannot resolve, but we will just ignore these."
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"frames_map['confirmed'].loc[\n",
" frames_map['confirmed'].index.levels[0].isin(data_pop_ser.index) == False\n",
"].iloc[:,-2:]"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Convert Series to Rates per 100,000
# Convert Series to Rates per 100,000
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
pandas
as
pd
import
pandas
as
pd
import
os
import
os
```
```
%% Cell type:code id: tags:parameters
%% Cell type:code id: tags:parameters
```
python
```
python
ts_folder
=
"
../data/covid-19_jhu-csse/
"
ts_folder
=
"
../data/covid-19_jhu-csse/
"
wb_path
=
"
../data/worldbank/SP.POP.TOTL.zip
"
wb_path
=
"
../data/worldbank/SP.POP.TOTL.zip
"
out_folder
=
None
out_folder
=
None
PAPERMILL_OUTPUT_PATH
=
None
PAPERMILL_OUTPUT_PATH
=
None
```
```
%% Cell type:markdown id: tags:parameters
%% Cell type:markdown id: tags:parameters
## Read in JHU CSSE data
## Read in JHU CSSE data
I will switch to
[
xarray
](
http://xarray.pydata.org/en/stable/
)
, but ATM, it's easier like this...
I will switch to
[
xarray
](
http://xarray.pydata.org/en/stable/
)
, but ATM, it's easier like this...
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
read_jhu_covid_region_df
(
name
):
def
read_jhu_covid_region_df
(
name
):
filename
=
os
.
path
.
join
(
ts_folder
,
f
"
time_series_19-covid-
{
name
}
.csv
"
)
filename
=
os
.
path
.
join
(
ts_folder
,
f
"
time_series_19-covid-
{
name
}
.csv
"
)
df
=
pd
.
read_csv
(
filename
)
df
=
pd
.
read_csv
(
filename
)
df
=
df
.
set_index
([
'
Country/Region
'
,
'
Province/State
'
,
'
Lat
'
,
'
Long
'
])
df
=
df
.
set_index
([
'
Country/Region
'
,
'
Province/State
'
,
'
Lat
'
,
'
Long
'
])
df
.
columns
=
pd
.
to_datetime
(
df
.
columns
)
df
.
columns
=
pd
.
to_datetime
(
df
.
columns
)
region_df
=
df
.
groupby
(
level
=
'
Country/Region
'
).
sum
()
region_df
=
df
.
groupby
(
level
=
'
Country/Region
'
).
sum
()
loc_df
=
df
.
reset_index
([
2
,
3
]).
groupby
(
level
=
'
Country/Region
'
).
mean
()[[
'
Long
'
,
'
Lat
'
]]
loc_df
=
df
.
reset_index
([
2
,
3
]).
groupby
(
level
=
'
Country/Region
'
).
mean
()[[
'
Long
'
,
'
Lat
'
]]
return
region_df
.
join
(
loc_df
).
set_index
([
'
Long
'
,
'
Lat
'
],
append
=
True
)
return
region_df
.
join
(
loc_df
).
set_index
([
'
Long
'
,
'
Lat
'
],
append
=
True
)
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
frames_map
=
{
frames_map
=
{
"
confirmed
"
:
read_jhu_covid_region_df
(
"
Confirmed
"
),
"
confirmed
"
:
read_jhu_covid_region_df
(
"
Confirmed
"
),
"
deaths
"
:
read_jhu_covid_region_df
(
"
Deaths
"
),
"
deaths
"
:
read_jhu_covid_region_df
(
"
Deaths
"
),
"
recovered
"
:
read_jhu_covid_region_df
(
"
Recovered
"
)
"
recovered
"
:
read_jhu_covid_region_df
(
"
Recovered
"
)
}
}
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
frames_map
[
'
confirmed
'
].
sort_values
(
frames_map
[
'
confirmed
'
].
columns
[
-
1
],
ascending
=
False
).
head
()
frames_map
[
'
confirmed
'
].
sort_values
(
frames_map
[
'
confirmed
'
].
columns
[
-
1
],
ascending
=
False
).
head
()
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Read in World Bank data
# Read in World Bank data
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
zipfile
import
zipfile
zf
=
zipfile
.
ZipFile
(
wb_path
)
zf
=
zipfile
.
ZipFile
(
wb_path
)
pop_df
=
pd
.
read_csv
(
zf
.
open
(
"
API_SP.POP.TOTL_DS2_en_csv_v2_821007.csv
"
),
skiprows
=
4
)
pop_df
=
pd
.
read_csv
(
zf
.
open
(
"
API_SP.POP.TOTL_DS2_en_csv_v2_821007.csv
"
),
skiprows
=
4
)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There is 2018 pop data for all countries/regions except Eritrea
There is 2018 pop data for all countries/regions except Eritrea
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
pop_df
[
pd
.
isna
(
pop_df
[
'
2018
'
])]
pop_df
[
pd
.
isna
(
pop_df
[
'
2018
'
])]
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Fix the country/region names that differ between the World Bank population data and the JHU CSSE data.
Fix the country/region names that differ between the World Bank population data and the JHU CSSE data.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
region_wb_jhu_map
=
{
region_wb_jhu_map
=
{
'
China
'
:
'
Mainland China
'
,
'
Brunei Darussalam
'
:
'
Brunei
'
,
'
Iran, Islamic Rep.
'
:
'
Iran (Islamic Republic of)
'
,
'
Czech Republic
'
:
'
Czechia
'
,
'
Korea, Rep.
'
:
'
Republic of Korea
'
,
'
United States
'
:
'
US
'
,
'
United Kingdom
'
:
'
UK
'
,
'
Hong Kong SAR, China
'
:
'
Hong Kong SAR
'
,
'
Egypt, Arab Rep.
'
:
'
Egypt
'
,
'
Egypt, Arab Rep.
'
:
'
Egypt
'
,
'
Vietnam
'
:
'
Viet Nam
'
,
'
Hong Kong SAR, China
'
:
'
Hong Kong SAR
'
,
'
Iran, Islamic Rep.
'
:
'
Iran
'
,
'
Korea, Rep.
'
:
'
Korea, South
'
,
'
Macao SAR, China
'
:
'
Macao SAR
'
,
'
Macao SAR, China
'
:
'
Macao SAR
'
,
'
Russian Federation
'
:
'
Russia
'
,
'
Slovak Republic
'
:
'
Slovakia
'
,
'
Slovak Republic
'
:
'
Slovakia
'
,
'
Moldova
'
:
'
Republic of Moldova
'
,
'
St. Martin (French part)
'
:
'
Saint Martin
'
,
'
St. Martin (French part)
'
:
'
Saint Martin
'
,
'
Brunei Darussalam
'
:
'
Brunei
'
'
United States
'
:
'
US
'
}
}
current_pop_ser
=
pop_df
[[
'
Country Name
'
,
'
2018
'
]].
copy
().
replace
(
region_wb_jhu_map
).
set_index
(
'
Country Name
'
)[
'
2018
'
]
current_pop_ser
=
pop_df
[[
'
Country Name
'
,
'
2018
'
]].
copy
().
replace
(
region_wb_jhu_map
).
set_index
(
'
Country Name
'
)[
'
2018
'
]
data_pop_ser
=
current_pop_ser
[
current_pop_ser
.
index
.
isin
(
frames_map
[
'
confirmed
'
].
index
.
levels
[
0
])]
data_pop_ser
=
current_pop_ser
[
current_pop_ser
.
index
.
isin
(
frames_map
[
'
confirmed
'
].
index
.
levels
[
0
])]
```
```
%% Cell type:code id: tags:
```
python
# Use this to find the name in the series
# current_pop_ser[current_pop_ser.index.str.contains('Czech')]
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There are some regions that we cannot resolve, but we will just ignore these.
There are some regions that we cannot resolve, but we will just ignore these.
%% Cell type:code id: tags:
```
python
frames_map
[
'
confirmed
'
].
loc
[
frames_map
[
'
confirmed
'
].
index
.
levels
[
0
].
isin
(
data_pop_ser
.
index
)
==
False
].
iloc
[:,
-
2
:]
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Compute rates per 100,000 for regions
# Compute rates per 100,000 for regions
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
cases_to_rates_df
(
df
):
def
cases_to_rates_df
(
df
):
per_100000_df
=
df
.
reset_index
([
1
,
2
],
drop
=
True
)
per_100000_df
=
df
.
reset_index
([
1
,
2
],
drop
=
True
)
per_100000_df
=
per_100000_df
.
div
(
data_pop_ser
,
'
index
'
).
mul
(
100000
).
dropna
()
per_100000_df
=
per_100000_df
.
div
(
data_pop_ser
,
'
index
'
).
mul
(
100000
).
dropna
()
per_100000_df
.
index
.
name
=
'
Country/Region
'
per_100000_df
.
index
.
name
=
'
Country/Region
'
return
per_100000_df
return
per_100000_df
def
frames_to_rates
(
frames_map
):
def
frames_to_rates
(
frames_map
):
return
{
k
:
cases_to_rates_df
(
v
)
for
k
,
v
in
frames_map
.
items
()}
return
{
k
:
cases_to_rates_df
(
v
)
for
k
,
v
in
frames_map
.
items
()}
rates_map
=
frames_to_rates
(
frames_map
)
rates_map
=
frames_to_rates
(
frames_map
)
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
if
PAPERMILL_OUTPUT_PATH
:
if
PAPERMILL_OUTPUT_PATH
:
for
k
,
v
in
rates_map
.
items
():
for
k
,
v
in
rates_map
.
items
():
out_path
=
os
.
path
.
join
(
out_folder
,
f
"
ts_rates_19-covid-
{
k
}
.csv
"
)
out_path
=
os
.
path
.
join
(
out_folder
,
f
"
ts_rates_19-covid-
{
k
}
.csv
"
)
v
.
reset_index
().
to_csv
(
out_path
)
v
.
reset_index
().
to_csv
(
out_path
)
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment