{ "cells": [ { "cell_type": "markdown", "id": "90700fdc-fcc7-4e54-8c9e-449879d8c66d", "metadata": { "tags": [] }, "source": [ "# World Bank Indicators API Example\n", "\n", "> The following is an example of a [Jupyter notebook](https://jupyter.org) - a tutorial of how to retrieve data from the [World Bank Indicators API](https://datahelpdesk.worldbank.org/knowledgebase/articles/889392-about-the-indicators-api-documentation) - that illustrates how to use computational content with the [template](https://worldbank.github.io/template). " ] }, { "cell_type": "markdown", "id": "e0d992a6-f656-45ce-a025-f824901e8797", "metadata": {}, "source": [ "## Requirements" ] }, { "cell_type": "code", "execution_count": 1, "id": "1811080b-c4c6-43cb-9e46-5cfa65d54abf", "metadata": {}, "outputs": [], "source": [ "import itertools\n", "\n", "import pandas\n", "import requests\n", "from bokeh.palettes import Spectral6\n", "from bokeh.plotting import figure, output_notebook, show" ] }, { "attachments": {}, "cell_type": "markdown", "id": "fb8d2738-535e-4957-b82a-987891955a7f", "metadata": {}, "source": [ "## Data Retrieval\n", "\n", "In this example, we retrieve **Population, total** (`SP.POP.TOTL`) from the [World Bank Indicators](https://data.worldbank.org/indicator) for [BRICS](https://infobrics.org)." ] }, { "cell_type": "code", "execution_count": 2, "id": "c955864a-1889-4f7f-a29e-108b0534846b", "metadata": {}, "outputs": [], "source": [ "url = \"https://api.worldbank.org/v2/country/chn;bra;ind;rus;zaf/indicator/SP.POP.TOTL?format=json&per_page=10000\"" ] }, { "cell_type": "markdown", "id": "6b5aac7c-bf80-4daa-a4a4-eebe8edc97bb", "metadata": {}, "source": [ "Let's use [requests](https://requests.readthedocs.io) to send a GET request," ] }, { "cell_type": "code", "execution_count": 3, "id": "8d699f28-853a-40a1-8ea9-8dd566962454", "metadata": {}, "outputs": [], "source": [ "r = requests.get(url)" ] }, { "cell_type": "markdown", "id": "a21cf193-ec13-45b8-9726-bb960ac8586a", "metadata": {}, "source": [ "Now, let's normalize and create `pandas.DataFrame` from the response," ] }, { "cell_type": "code", "execution_count": 4, "id": "3dc152b2-95ba-473a-a416-2c4d5bac7622", "metadata": {}, "outputs": [], "source": [ "# normalize\n", "data = pandas.json_normalize(r.json()[-1])\n", "\n", "# create dataframe\n", "df = pandas.DataFrame.from_dict(data)" ] }, { "cell_type": "markdown", "id": "241904c0-35b9-4e43-a3f9-f97738ea9fd1", "metadata": {}, "source": [ "```{tip}\n", "Alternatively, the World Bank API supports downloading the data as an [archive](http://api.worldbank.org/v2/country/all/indicator/SP.POP.TOTL?date=2000&source=2&downloadformat=csv). \n", "```" ] }, { "cell_type": "markdown", "id": "bae3462b-f49c-4b8a-badb-8f580b4fc268", "metadata": {}, "source": [ "Let's take a look at the dataframe, " ] }, { "cell_type": "code", "execution_count": 5, "id": "c0bbef2d-495c-4140-b8ac-45ee47772142", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
countryiso3code | \n", "BRA | \n", "CHN | \n", "IND | \n", "RUS | \n", "ZAF | \n", "
---|---|---|---|---|---|
date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " |
1960 | \n", "73.092515 | \n", "667.070 | \n", "445.954579 | \n", "119.897000 | \n", "16.520441 | \n", "
1961 | \n", "75.330008 | \n", "660.330 | \n", "456.351876 | \n", "121.236000 | \n", "16.989464 | \n", "
1962 | \n", "77.599218 | \n", "665.770 | \n", "467.024193 | \n", "122.591000 | \n", "17.503133 | \n", "
1963 | \n", "79.915555 | \n", "682.335 | \n", "477.933619 | \n", "123.960000 | \n", "18.042215 | \n", "
1964 | \n", "82.262794 | \n", "698.355 | \n", "489.059309 | \n", "125.345000 | \n", "18.603097 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
2018 | \n", "210.166592 | \n", "1402.760 | \n", "1369.003306 | \n", "144.477859 | \n", "57.339635 | \n", "
2019 | \n", "211.782878 | \n", "1407.745 | \n", "1383.112050 | \n", "144.406261 | \n", "58.087055 | \n", "
2020 | \n", "213.196304 | \n", "1411.100 | \n", "1396.387127 | \n", "144.073139 | \n", "58.801927 | \n", "
2021 | \n", "214.326223 | \n", "1412.360 | \n", "1407.563842 | \n", "144.130482 | \n", "59.392255 | \n", "
2022 | \n", "215.313498 | \n", "1412.175 | \n", "1417.173173 | \n", "144.236933 | \n", "59.893885 | \n", "
63 rows × 5 columns
\n", "\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"
\\n\"+\n",
" \"\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"
\\n\"+\n \"