1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonを使って世界銀行のデータにアクセスする方法のメモ。World Bank APIを利用

Last updated at Posted at 2023-07-28

Pythonを使って世界銀行のデータにアクセスする方法のメモ。World Bank APIを利用します

  • World Bank APIは、世界銀行が提供する公開データへのアクセスを提供するウェブサービスです。このAPIを使用すると、開発指標、貸付データ、経済データなど、世界銀行の広範なデータベースにアクセスできます。これらのデータは、経済開発や政策決定に関する研究や分析に役立ちます。

まず、試しに行ったのは、以下です


World Bank APIを試しに使う

このコードは、World Bank APIから日本のGDPデータを取得し、それをPandasのデータフレームに変換して表示するもの。

  • requests.get(url)でAPIからデータを取得します。
  • response.json()で取得したデータをJSON形式に変換します。
  • pd.json_normalize(data[1])でJSONデータをPandasのデータフレームに変換します。
  • df.head()でデータフレームの最初の5行を表示します。

import requests
import pandas as pd

# APIのURL
url = "http://api.worldbank.org/v2/country/JP/indicator/NY.GDP.MKTP.CD?format=json"

# APIからデータを取得
response = requests.get(url)
data = response.json()

# データをデータフレームに変換
df = pd.json_normalize(data[1])

print(df.head())

ISO 3166の国コードのリストはWikipediaで見ることができます。

Indicator API Queriesのページには、APIを使って指標を取得する方法が詳しく説明されています。指標は、総人口、国民総所得、エネルギー使用量などのデータを表します。

指標を問い合わせると、以下の情報がレスポンスに含まれます:

  • コード
  • 名前
  • 単位
  • ソースID
  • ソースノート
  • ソース組織
  • トピックID
  • トピック名

すべての指標をリクエストするには、次のURLを使用します:http://api.worldbank.org/v2/indicator

特定の指標(例えば、GDP(現在のUS$))をリクエストするには、その指標コード(この場合はNY.GDP.MKTP.CD)を使用します:http://api.worldbank.org/v2/indicator/NY.GDP.MKTP.CD

指標が複数のソースに属している場合があります。特定のソースから指標を見つけるには、クエリパラメータとして指標のソースIDを提供する必要があります。例えば:

複数の国のデータを取得するには、URLの"country"パラメータを変更します。国コードはISO 3166-1 alpha-2形式で、カンマで区切って複数指定できます。例えば、日本(JP)とアメリカ(US)とイギリス(GB)のデータを取得するには以下のようにします。

url = "http://api.worldbank.org/v2/country/JP;US;GB/indicator/NY.GDP.MKTP.CD?format=json"

このURLは、日本、アメリカ、イギリスのGDPデータを取得します。


また、他の例もあるかChatGPTに問い合わせたら、以下リンクを教えてくれました。

おいおい確認


  1. Introducing WBGAPI: A new python package for accessing World Bank data
  2. Accessing the World Bank Data APIs in Python, R, Ruby & Stata
  3. Python: How to use World Bank API (WBGAPI) and Pandas to import data, and draw charts (Tutorial) - YouTube
  4. Accessing International Debt Statistics (IDS) through World Bank Data API
  5. world-bank-data - PyPI
  6. wbgapi - PyPI
  7. Analyzing Global Population Data with Python and the World Bank API | by Brecht Corbeel
  8. world-bank-api · GitHub Topics
  9. python - Getting data from World Bank API using pandas - Stack Overflow
  10. [PYTHON] World Bank data - Deepnote

これらのリンクは、世界銀行のAPIをPythonで使用する方法についての情報を提供しています。具体的なコード例やチュートリアルも含まれています


1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?