# World Bank API in pandas, with Eurostat and ECB

Call the World Bank API, Eurostat and the ECB from Python, turn three JSON formats into one pandas DataFrame, and see why their inflation numbers differ.

## Summary

The World Bank API, Eurostat and the European Central Bank all publish inflation figures for free, but putting them side by side in pandas takes more work than the data suggests, because each one answers in its own format. The [World Bank Indicators API](https://datahelpdesk.worldbank.org/knowledgebase/articles/889392-about-the-indicators-api-documentation) sends a two-item array, the [Eurostat statistics API](https://ec.europa.eu/eurostat/web/user-guides/data-browser/api-data-access/api-getting-started/api) sends [JSON-stat](https://json-stat.org/format/) with values in a flat list, and the [ECB Data Portal API](https://data.ecb.europa.eu/help/api/overview) sends SDMX-JSON with values nested under series keys. Most of the effort in a comparison like this goes into reading those three formats.

This guide pulls annual inflation for Germany, France and Spain from all three sources through one looot key, turns the answers into one tidy DataFrame, and then looks at the most interesting thing in the result: the sources disagree. Germany's inflation in 2022 was 8.7% according to Eurostat and 6.9% according to the World Bank, and neither of them is wrong.

## How do you call the World Bank API from Python, with Eurostat and the ECB?

You send the same request to `POST /v1/runs` each time and change only the endpoint and its input, so authentication and retries live in one small function. Each answer still comes back in the provider's own format, which is why each source below gets its own short parser. Install the two libraries with `pip install pandas requests`, set `LOOOT_TOKEN`, and run this script.

The last line should print 15 for each source: three countries times five years. All three sources use the same two-letter country codes in their answers (DE, FR, ES), which is what lets one `country` column join them. If a provider has a bad moment, `run()` waits and tries again up to three times. Each attempt sends a fresh idempotency key, because a key you already used returns the stored run, including a failed one.

The Eurostat parser rebuilds each row's position from the sizes of the dimensions, because JSON-stat stores values in one flat list ordered by the dimensions. In the ECB call, set `lastNObservations` along with the dates, because the endpoint returns only the most recent 12 observations per series by default. The ECB figures here are monthly rates, so the script averages them by year. That annual average is our own calculation from ECB statistics, not a number the ECB publishes.

## Why do the World Bank and Eurostat numbers disagree?

They measure different things. The World Bank's [FP.CPI.TOTL.ZG](https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG) indicator is consumer price inflation built from each country's national consumer price index. Eurostat's dataset and the ECB's series both use the [Harmonised Index of Consumer Prices](https://ec.europa.eu/eurostat/web/hicp), or HICP, which Eurostat describes as a measure of inflation that is comparable across countries, compiled under common [methodology](https://ec.europa.eu/eurostat/web/hicp/methodology). That is why Eurostat and the ECB land within 0.1 of each other in every row, while the World Bank moves further away in some years.

| Country and year | ECB (average of monthly rates) | Eurostat (HICP) | World Bank (CPI) |
|---|---|---|---|
| Germany 2022 | 8.6 | 8.7 | 6.9 |
| France 2023 | 5.7 | 5.7 | 4.9 |
| Spain 2022 | 8.3 | 8.3 | 8.4 |
| Germany 2024 | 2.5 | 2.5 | 2.3 |

Pick the measure that fits your question before you merge anything. If you compare EU countries with each other, HICP is the one designed for that. If you need the same indicator across the whole world, the World Bank series covers countries that HICP does not. Averaging the sources together would give you a number that none of them published.

## Do you need looot to call the World Bank API?

No. The World Bank Indicators API needs no key, and for one source a direct call is the simplest option. Countries are joined with a semicolon and a year range uses a colon, as the [API call structure page](https://datahelpdesk.worldbank.org/knowledgebase/articles/898581-api-basic-call-structures) shows:

The answer is a list of two items: paging information first, then the rows. Ask for a date range or raise `per_page`, because the API returns 50 rows per page by default. looot earns its place when you combine sources: the World Bank, Eurostat and the ECB, plus paid data when you need it, all behind one key, one request shape and one retry function.

## How do you extend this to other series?

Each parser only cares about the shape of the answer, so you can swap in another series by changing the input. The [World Bank indicator series endpoint](https://looot.ai/public-catalog/world-bank-indicator-series) takes other indicator codes, such as `SL.UEM.TOTL.ZS` for unemployment, and `world-bank-indicators` lists the codes in a source database. The [Eurostat dataset query](https://looot.ai/public-catalog/eurostat-dataset-query) takes other dataset codes along with their dimension filters, and those filters differ between datasets, so check them for each one. Eurostat labels `prc_hicp_aind` as covering 1996 to 2025. For newer figures, use its successor `prc_hicp_ainr` (annual) or `prc_hicp_minr` (monthly), which filter on `coicop18` with the code `TOTAL` instead of `coicop` with `CP00`. The [ECB series endpoint](https://looot.ai/public-catalog/ecb-series) takes any dataflow and key shown on the ECB Data Portal, such as exchange rates in `EXR`. All three are free through looot.

## What this does not do

looot passes along each provider's own answer and does not reconcile them, so country codes, units and definitions stay yours to check. Check the reuse terms too: most World Bank indicators are licensed CC BY 4.0, but a few, this inflation series among them, list a third-party source with its own terms, so look at the indicator's source before you reuse it commercially. Eurostat and the ECB both ask you to credit them, and the ECB asks that its statistics are not changed, which is why the averages above are labelled as ours. For how looot handles jobs that several providers can answer, see [how data provider fallback works](https://looot.ai/blog/automatic-provider-fallback-for-data-apis), and for the case for one key across many data APIs, see [one API key for many data APIs](https://looot.ai/blog/one-api-key-for-many-data-apis).

## Try it: build your own comparison

Data: World Bank, World Development Indicators. Eurostat. ECB statistics, with annual averages calculated by looot from the monthly rates. [See what a call costs](https://looot.ai/pricing).

## Questions

### Is the World Bank API free?

Yes. The World Bank Indicators API needs no key and charges nothing. Most of its data is licensed CC BY 4.0, which requires attribution, and a few indicators from third-party sources carry their own terms. It is also free through looot.

### How do I use the World Bank API in Python?

Send a GET request with requests to https://api.worldbank.org/v2/country/DEU;FRA;ESP/indicator/FP.CPI.TOTL.ZG?format=json&date=2019:2024 and read the second item of the answer, which holds the rows. The direct-call snippet above turns them into a DataFrame with pandas.json_normalize. looot returns the same answer through the world-bank-indicator-series endpoint, with the same key you use for Eurostat and the ECB.

### What is the difference between HICP and CPI?

HICP is the EU's harmonised measure, built so that inflation is comparable across countries. A national CPI, which the World Bank series uses, can use a different basket and rules, so the two can differ by more than a point in the same year.

### Why use looot for free APIs?

One key and one request format cover all of them, along with paid sources when you need them, and every run is recorded with its cost. If you only need one free API, calling it directly works well.

### Can I load the World Bank API answer straight into pandas?

Yes, once you take the rows out of the two-item answer. The answers from the three sources are JSON in three different structures, so pandas.read_json alone will not flatten them, which is why the script has one short parser per source.

### Does this work for countries outside Europe?

The World Bank series does. Eurostat and the ECB mainly cover Europe, so a global comparison should rest on the World Bank.

## For agents

### Code (python)

```python
import itertools, os, time, uuid
import pandas as pd
import requests

API = "https://api.looot.ai/v1/runs"
HEADERS = {"Authorization": f"Bearer {os.environ['LOOOT_TOKEN']}"}

def run(endpoint, inputs, attempts=3):
    for attempt in range(attempts):
        response = requests.post(
            API, params={"wait": 40}, headers=HEADERS, timeout=70,
            json={"endpointId": endpoint, "input": inputs,
                  "idempotencyKey": str(uuid.uuid4())},
        )
        answer = response.json()
        if answer.get("outcome") == "hit":
            return answer["result"]
        time.sleep(2 * (attempt + 1))
    raise RuntimeError(f"{endpoint}: {answer.get('outcome')} {answer.get('error')}")

def world_bank():
    result = run("world-bank-indicator-series", {
        "countries": "DEU;FRA;ESP", "indicator": "FP.CPI.TOTL.ZG",
        "date": "2019:2024", "per_page": 100,
    })
    rows = result[1]
    return pd.DataFrame({
        "country": [r["country"]["id"] for r in rows],
        "year": [int(r["date"]) for r in rows],
        "value": [r["value"] for r in rows],
    }).assign(source="World Bank")

def eurostat():
    result = run("eurostat-dataset-query", {
        "dataset": "prc_hicp_aind", "geo": ["DE", "FR", "ES"],
        "unit": ["RCH_A_AVG"], "coicop": ["CP00"], "lastTimePeriod": 6,
    })
    geo = result["dimension"]["geo"]["category"]["index"]
    years = result["dimension"]["time"]["category"]["index"]
    rows = []
    for (country, i), (year, j) in itertools.product(geo.items(), years.items()):
        value = result["value"].get(str(i * len(years) + j))
        rows.append({"country": country, "year": int(year), "value": value})
    return pd.DataFrame(rows).assign(source="Eurostat")

def ecb():
    result = run("ecb-series", {
        "dataflow": "ICP", "key": "M.DE+FR+ES.N.000000.4.ANR",
        "startPeriod": "2019-01", "endPeriod": "2024-12", "lastNObservations": 72,
    })
    structure = result["structure"]["dimensions"]
    countries = next(d for d in structure["series"] if d["id"] == "REF_AREA")["values"]
    position = next(i for i, d in enumerate(structure["series"]) if d["id"] == "REF_AREA")
    periods = structure["observation"][0]["values"]
    rows = []
    for key, series in result["dataSets"][0]["series"].items():
        country = countries[int(key.split(":")[position])]["id"]
        for index, obs in series["observations"].items():
            rows.append({"country": country,
                         "year": int(periods[int(index)]["id"][:4]),
                         "value": obs[0]})
    monthly = pd.DataFrame(rows)
    return (monthly.groupby(["country", "year"], as_index=False)["value"].mean()
                   .assign(source="ECB"))

data = pd.concat([world_bank(), eurostat(), ecb()], ignore_index=True)
data = data[data["year"].between(2020, 2024)]
table = data.pivot_table(index=["country", "year"], columns="source", values="value").round(1)
print(table.to_string())
print(data.groupby("source")["value"].count().to_string())
```

### Code (python)

```python
import pandas as pd
import requests

url = "https://api.worldbank.org/v2/country/DEU;FRA;ESP/indicator/FP.CPI.TOTL.ZG"
paging, rows = requests.get(url, params={"format": "json", "date": "2019:2024", "per_page": 100}, timeout=60).json()
frame = pd.json_normalize(rows)[["countryiso3code", "date", "value"]]
print(frame.head())
```

### Compare one indicator across sources

```text
Use looot to compare one economic indicator across sources.
1. Set up https://looot.ai/skill.md.
2. Pull annual inflation for the countries I give you from world-bank-indicator-series, eurostat-dataset-query and ecb-series.
3. Build one pandas DataFrame with country, year, source and value, and show me where the sources differ by more than one point.
4. Tell me what each source measures before you draw any conclusion.
```
