LoginSignup
4
6

More than 1 year has passed since last update.

[Python] World Bankのdatasetから世界各国の人口データをdataframeにて入手する

Last updated at Posted at 2021-02-27

時々、世界各国の人口データを使いたいときがあったのですが、Wikipediaなどからエクセルにコピーして読み込まないといけませんでした。

しかし、最近、World bankより人口を含んだデータセットが提供されていることが分かり、データフレームとして簡単に取り込むことができたので、自分の忘備録として記録に残します。
なお、私はWindows10でJupyterNotebookにてPythonを利用しています。
まず、pipでインストールします。

pip install world_bank_data --upgrade

次に、こちらのコードを実行します。

import pandas as pd
import world_bank_data as wb
pd.set_option('display.max_rows', 20)
# Same data set, indexed with the country code
population = wb.get_series('SP.POP.TOTL', id_or_value='id', simplify_index=True, mrv=1)
countries = wb.get_countries()
# Aggregate region, country and population
df = countries[['region', 'name']].rename(columns={'name': 'country'}).loc[countries.region != 'Aggregates']
df['population'] = population
df['population'] =df['population'].fillna(0).astype(int) #浮動小数点n表示になるのでintに変換
df['population_Oku'] = population/100000000
df.sort_values('population', ascending = False).head(5)

Pandasで読み込むと・・
スクリーンショット 2022-06-20 181608.png
そうすると、このようなに世界の人口Top5を表示できます。
{F83522EE-032B-4A32-8F75-397FE81B3A8C}.png.jpg
アジアの人口トップ10も出してみました。
ここで、次のようにします。

df[df['region'].str.contains("East Asia")].sort_values('population', ascending = False).head(10)

{3C777963-946B-40E2-BD22-AA96706F9C1A}.png.jpg
こちらの英語のサイトを参考にしています。
ページの一番下のplottyで作ってあるチャートはなかなかいいなと思いました。
https://nbviewer.jupyter.org/github/mwouts/world_bank_data/blob/master/examples/A%20sunburst%20plot%20of%20the%20world%20population.ipynb

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6