I.何をする?

COVID-19 Data Hubのデータより各国のワクチンの接種数を比較する表を作成します。

https://covid19datahub.io/articles/api/python.html

II.データの取得

使用するライブラリをインストールします。

# conda install -c conda-forge klib
from covid19dh import covid19
import klib
import pandas as pd
from matplotlib import pyplot as plt

データを取得します。xがデータで、srcは出所一覧です。

x, src = covid19(verbose=False)
# show the first 5 rows
x.head()
##     id       date  vaccines  ...  key  key_apple_mobility  key_google_mobility
## 0  AFG 2020-01-22       NaN  ...  NaN                 NaN                   AF
## 1  AFG 2020-01-23       NaN  ...  NaN                 NaN                   AF
## 2  AFG 2020-01-24       NaN  ...  NaN                 NaN                   AF
## 3  AFG 2020-01-25       NaN  ...  NaN                 NaN                   AF
## 4  AFG 2020-01-26       NaN  ...  NaN                 NaN                   AF
## 
## [5 rows x 36 columns]

列名を取得します。

list(x)
## ['id', 'date', 'vaccines', 'tests', 'confirmed', 'recovered', 'deaths', 'hosp', 'vent', 'icu', 'population', 'school_closing', 'workplace_closing', 'cancel_events', 'gatherings_restrictions', 'transport_closing', 'stay_home_restrictions', 'internal_movement_restrictions', 'international_movement_restrictions', 'information_campaigns', 'testing_policy', 'contact_tracing', 'stringency_index', 'iso_alpha_3', 'iso_alpha_2', 'iso_numeric', 'currency', 'administrative_area_level', 'administrative_area_level_1', 'administrative_area_level_2', 'administrative_area_level_3', 'latitude', 'longitude', 'key', 'key_apple_mobility', 'key_google_mobility']

III.欠損値を示すグラフを作成

klibを使って欠損値の分布をグラフにします。黒い部分が欠損値です。

国ごとに上かが日付順に並んでいます。vaccinesの列をみると欠損値が非常に多いことがわかります。当初はワクチンがなかなったので欠損値が多くなるのは当然ですが、一番下を見ると、直近にも欠損値が散見されます。

x, src = covid19(verbose=False)
klib.missingval_plot(x)
## GridSpec(6, 6)
plt.show()

IV.接種数を降順に並べる

欠損値があるので、日付でなく、それぞれの国のワクチン接種数の最大値を取り出し降順に並べます。

コードの末尾に、reset_index()を加えるとインデクス(0から始まる連番)がふられ、データ・フレームとして取得できます。コードは次です。

x_vaccines = x.groupby(['id'])['vaccines'].max().reset_index()
x_vaccines.head()
##     id   vaccines
## 0  AFG    54000.0
## 1  AGO    87022.0
## 2  ALB    64075.0
## 3  AND     9288.0
## 4  ARE  8081751.0

sort_values( )で降順に並べます。

日本は41位で、ノルウェー、ドミニカ共和国の間ですが、Wikipediaによればノルウェーの人口は5,391,369人、ドミニカ共和国の人口は71,625です。

x_vaccines_descend = x_vaccines.sort_values(by='vaccines', ascending=False).reset_index()
x_vaccines_descend.head(50)
##     index   id     vaccines
## 0     186  USA  143462691.0
## 1      82  IND   60530435.0
## 2      64  GBR   33678768.0
## 3      25  BRA   21154088.0
## 4     180  TUR   14652659.0
## 5      47  DEU   12872859.0
## 6     152  RUS   10600000.0
## 7      81  IDN   10425690.0
## 8      62  FRA   10394243.0
## 9      87  ISR    9934359.0
## 10     34  CHL    9646735.0
## 11     88  ITA    9413886.0
## 12      4  ARE    8081751.0
## 13    110  MAR    7634475.0
## 14     57  ESP    7067371.0
## 15    115  MEX    6852596.0
## 16    144  POL    5882581.0
## 17     16  BGD    5139456.0
## 18     32  CAN    5113935.0
## 19    154  SAU    4125466.0
## 20      5  ARG    3625283.0
## 21    151  ROU    2858442.0
## 22     80  HUN    2609739.0
## 23    163  SRB    2323689.0
## 24    134  NLD    2097980.0
## 25     13  BEL    1732032.0
## 26     40  COL    1726924.0
## 27     72  GRC    1604238.0
## 28    136  NPL    1600000.0
## 29    146  PRT    1599599.0
## 30     46  CZE    1572181.0
## 31     10  AUT    1544597.0
## 32    169  SWE    1482153.0
## 33     33  CHE    1342748.0
## 34    157  SGP    1109000.0
## 35     50  DNK    1045626.0
## 36     60  FIN     921123.0
## 37    167  SVK     899660.0
## 38    105  LKA     894053.0
## 39    135  NOR     865800.0
## 40     91  JPN     822869.0
## 41    141  PER     801867.0
## 42     51  DOM     800000.0
## 43     97  KOR     799198.0
## 44     83  IRL     760168.0
## 45    149  QAT     740309.0
## 46     18  BHR     735717.0
## 47    129  MYS     566200.0
## 48    185  URY     510638.0
## 49     11  AZE     510420.0

V.接種数を各国人口比の降順に並べる

必要な列を選びます。

x_selected = x[['id', 'date', 'vaccines', 'population']]
x_selected.head()
##     id       date  vaccines  population
## 0  AFG 2020-01-22       NaN    37172386
## 1  AFG 2020-01-23       NaN    37172386
## 2  AFG 2020-01-24       NaN    37172386
## 3  AFG 2020-01-25       NaN    37172386
## 4  AFG 2020-01-26       NaN    37172386

人口10万人あたり比ワクチン接種者数を算出します。警告がでますが、これについては次の解説を参考にして別に書く予定です。

SettingWithCopyWarning in Pandas: Views vs Copies

x_selected['vaccines_pop'] = x_selected['vaccines'] / x_selected['population'] * 100000
## /Users/hirofumishibata/anaconda3/anaconda3/envs/nla/bin/python3:1: SettingWithCopyWarning: 
## A value is trying to be set on a copy of a slice from a DataFrame.
## Try using .loc[row_indexer,col_indexer] = value instead
## 
## See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
x_selected.head()
##     id       date  vaccines  population  vaccines_pop
## 0  AFG 2020-01-22       NaN    37172386           NaN
## 1  AFG 2020-01-23       NaN    37172386           NaN
## 2  AFG 2020-01-24       NaN    37172386           NaN
## 3  AFG 2020-01-25       NaN    37172386           NaN
## 4  AFG 2020-01-26       NaN    37172386           NaN

国別の最大値を算出し、降順に並べます。日本は97番目だとわかります。

x_selected_vaccines_pop = x_selected.groupby(['id'])['vaccines_pop'].max().reset_index().sort_values(by = 'vaccines_pop', ascending=False).reset_index()
x_selected_vaccines_pop.head(100)
##     index   id   vaccines_pop
## 0      87  ISR  111838.147881
## 1     171  SYC   99753.002212
## 2       4  ARE   83914.291401
## 3      34  CHL   51506.501092
## 4      64  GBR   50674.982964
## ..    ...  ...            ...
## 95    120  MMR     707.524401
## 96     91  JPN     650.339724
## 97    128  MWI     615.058494
## 98     92  KAZ     601.972480
## 99     99  LAO     576.817385
## 
## [100 rows x 3 columns]