Getting data

Every function here returns one row per value with country, year, value, unit, indicator_name and source, unless it says otherwise.

get(indicators, countries, years=None, *, by=None, wide=False)[source]

Get an indicator’s values for some countries, for every year or a range of years.

Parameters:
  • indicators (str | Sequence[str]) – One indicator id, or a list. An SDG indicator number such as "3.1.1" works too. Find ids with undata.search().

  • countries (str | Iterable[str]) – Country names, ISO codes, regions or groups, alone or in a list: "Nepal", ["Bangladesh", "Viet Nam"], "Southern Asia", "LDCs".

  • years (int | tuple[int, int] | range | Sequence[int] | None) – Leave out for every year. 2020 for one year, (2015, 2023) for an inclusive range, or a list of years.

  • by (str | None) – A breakdown, such as "sex" or "age". Adds a column with the breakdown’s values; see undata.breakdowns() for what exists.

  • wide (bool) – Return one column per indicator instead of one row per value.

Returns:

A table with one row per country, indicator and year. Countries with no data are left out and named in a MissingDataWarning.

Return type:

DataFrame

Example

>>> import undata as un
>>> mmr = "undata/sdg/SH_STA_MORT.SEX--F"
>>> un.get(mmr, ["Bangladesh", "Nepal"], years=(2020, 2023))
      country iso3  year       value                          unit  ...
0  Bangladesh  BGD  2020  151.84625  Count per 100,000 live births  ...
latest(indicators, countries, *, within=None, since=None, wide=False)[source]

Get each country’s most recent value for an indicator.

Many series are not published every year for every country, so “most recent” is per country: one country’s latest value may be from 2023 and another’s from 2015. The year column says which. To keep only recent enough values, say how far back to look: within=10 takes each country’s latest value from the past ten years, and names the countries whose latest is older.

Parameters:
  • indicators (str | Sequence[str]) – One indicator id or SDG indicator number, or a list of them.

  • countries (str | Iterable[str]) – Country names, ISO codes, regions or groups, alone or in a list.

  • within (int | None) – Look back this many years from this year, so within=5 in 2026 keeps values from 2021 on. Older values are left out and named in a StaleDataWarning.

  • since (int | None) – The same, as a year: since=2020 keeps values from 2020 on.

  • wide (bool) – Return one column per indicator instead of one row per value.

Returns:

A table with one row per country and indicator, in the same columns as get(). .attrs["window"] records the years looked at, and undata.coverage() summarises who has a value, who was too old and who has none.

Return type:

DataFrame

Example

>>> import undata as un
>>> table = un.latest("3.1.1", "Western Africa", within=5)
>>> un.coverage(table)
totals(indicators, places, years=None, *, latest=False, wide=False)[source]

Get the official figure for a region or group, as the agency published it.

get() treats "Africa" as every country in Africa. This asks for Africa itself: the regional total or average the agency computed with its own method. undata never computes one of its own. The country column holds the region’s name.

Parameters:
  • indicators (str | Sequence[str]) – One indicator id or SDG indicator number, or a list of them.

  • places (str | Iterable[str]) – Regions or groups such as "World", "Africa", "LDCs". Countries work too, and give the country’s own value.

  • years (int | tuple[int, int] | range | Sequence[int] | None) – As for get().

  • latest (bool) – Keep only each place’s most recent value.

  • wide (bool) – Return one column per indicator instead of one row per value.

Example

>>> import undata as un
>>> un.totals("3.1.1", ["World", "LDCs", "SIDS"], latest=True)
change(indicators, countries, start, end)[source]

Compare two years: the value in each, the difference, and the percentage change.

Only countries with a value in both years are in the table; the rest are named in a MissingDataWarning.

Parameters:
  • indicators (str | Sequence[str]) – One indicator id or SDG indicator number, or a list of them.

  • countries (str | Iterable[str]) – Country names, ISO codes, regions or groups.

  • start (int) – The earlier year, such as 2015 for the SDG baseline.

  • end (int) – The later year.

Returns:

One row per country and indicator – start_value, end_value, change and change_pct.

Return type:

DataFrame

Example

>>> import undata as un
>>> un.change("3.1.1", "Southern Asia", 2015, 2023)
compare(indicators, countries, *, within=None, since=None)[source]

Put several indicators side by side, each country’s latest value of each.

Before two indicators go on one chart, the question is who has both. A CoverageWarning answers it: how many countries have every indicator, and which have only some. Countries with none are left out.

Parameters:
  • indicators (Sequence[str] | Mapping[str, str]) – Indicator ids, or a mapping of short labels to ids, such as {"conflict": "undata/sdg/VC_DTH_TOTR", "homicide": "undata/sdg/VC_IHR_PSRC"}. Labels become the column names; without them, indicator names are used.

  • countries (str | Iterable[str]) – Country names, ISO codes, regions or groups.

  • within (int | None) – As for latest(): each country’s latest value from the past this many years.

  • since (int | None) – As for latest().

Returns:

One row per country with a value column and a year column per indicator. .attrs["coverage"] holds who has what.

Return type:

DataFrame

Example

>>> import undata as un
>>> un.compare({"conflict": conflict, "homicide": homicide}, "World")
wide(table)[source]

Turn a table into one column per indicator, one row per country (and year).

Parameters:

table (DataFrame) – A table from get(), latest() or totals().

Returns:

The values, with countries down the side and indicator names across the top. A table that has several years per country keeps a year level in the index. The original table stays attached, so undata.cite() and undata.save() still know the sources.

Return type:

DataFrame

Example

>>> import undata as un
>>> un.wide(un.latest([electricity, water], "Southern Asia"))