Sentences, files and citations¶
- describe(table)[source]¶
Write plain-English sentences about a table from
undata.For a table of latest values or totals: the highest, the lowest and the median, with the years they come from. For several years: how each country moved from its first year to its last. For
undata.change(): how many fell or rose, and the largest moves. Countries with no data are mentioned, not counted as zero.- Parameters:
table (DataFrame) – A table from
undata.get(),undata.latest(),undata.totals(),undata.change()orundata.wide(), or fromundata.progress(),undata.gap()orundata.profile(), which get their own summary: statuses, the widest gap, or ranks.- Returns:
A paragraph per indicator, ready to paste and edit.
- Return type:
Example
>>> import undata as un >>> table = un.latest("undata/sdg/EG_ACS_ELEC", "Southern Asia") >>> print(un.describe(table))
- coverage(table)[source]¶
Say how complete a table is: who has a value, who was too old, who has none.
- Parameters:
table (DataFrame) – A table from
undata.latest()(with or withoutwithin=),undata.get(),undata.totals()orundata.wide().- Returns:
One row per indicator – how many places were asked for, how many have a value, how many had only values older than the window, how many have none at all, the oldest, newest and median year of the values kept, and the window.
- Return type:
Example
>>> import undata as un >>> table = un.latest("3.1.1", "LDCs", within=5) >>> un.coverage(table)
- cite(table)[source]¶
Write a citation for every indicator and source in a table.
Credit goes to the agency that produced the data, with the platform as where it was retrieved.
- Parameters:
table (DataFrame) – A table from
undata.get()orundata.latest().- Returns:
One citation per line. Print it, or paste it under a chart.
- Return type:
Example
>>> import undata as un >>> print(un.cite(table)) SDG Global Database. Maternal mortality ratio [Sex=Female] ...
- save(table, path)[source]¶
Save a table to Excel or CSV, with its sources.
An
.xlsxfile gets two sheets:data, andsourceswith a citation for each indicator and the list of countries that had no data. A.csvfile holds the data; every row already carries its source.- Parameters:
table (DataFrame) – A table from
undata.get()orundata.latest().path (str | Path) – Where to save it, ending in
.xlsxor.csv.
- Returns:
The path written.
- Return type:
Example
>>> import undata as un >>> un.save(table, "maternal-mortality.xlsx")
For AI assistants and Power BI¶
To let an assistant fetch data itself, use the The AI connector (MCP). To hand it a table you already have, use these.
- for_ai(table, *, task=None, question=None, max_rows=200)[source]¶
Format a table for an AI assistant, with its sources, caveats and a task.
Paste the result into Copilot, ChatGPT or Claude. It tells the assistant to use only these numbers, to name the source and year, and that a missing country has no data rather than a value of zero. Asking an assistant what a statistic is invites a made-up number; giving it the statistic and asking what it means does not.
- Parameters:
table (DataFrame) – A table from
undata.get(),undata.latest(),undata.totals(),undata.change()orundata.wide().task (str | None) – What to ask for:
"explain","brief","trends","gaps"or"chart". Leave it out to ask your own question afterwards.question (str | None) – Your own question, added after the data.
max_rows (int) – Rows beyond this are cut, and the cut is stated.
- Returns:
Markdown text, ready to paste.
- Return type:
Example
>>> import undata as un >>> print(un.for_ai(un.latest("3.1.1", "Southern Asia"), task="brief"))
- ai_pack(table, path, *, task=None)[source]¶
Write a data pack to upload to an AI assistant: the data, what it means, a prompt.
Upload the pack (or its three files) to ChatGPT, Claude or Copilot, then paste
prompt.mdas your message. The assistant gets the numbers as data rather than pasted text, so it can calculate with them, and the metadata keeps it honest about units, sources, years and gaps.The pack holds:
data.csv- one row per value, with unit and source on every row.metadata.md- what each column means, each indicator’s unit, source and citation, how complete the data is, and the caveats.prompt.md- rules for the assistant, and a task or suggested questions.
- Parameters:
table (DataFrame) – A table from
undata.get(),undata.latest(),undata.totals(),undata.change()orundata.wide().path (str | Path) – A
.zipfile, or a folder to write the three files into.task (str | None) – As for
for_ai(). Left out, the prompt lists questions to choose from.
- Returns:
The path written.
- Return type:
Example
>>> import undata as un >>> un.ai_pack(un.latest("3.1.1", "LDCs", within=10), "maternal-mortality.zip")
- powerquery(indicators, countries, years=None)[source]¶
Write Power Query (M) code that fetches the same data, for Power BI or Excel.
In Power BI: Get data, Blank query, Advanced editor, then paste. In Excel: Data, Get Data, From Other Sources, Blank Query, Advanced Editor. The query refreshes on its own schedule after that, with no Python involved.
- Parameters:
- Returns:
M code, ready to paste.
- Return type:
Example
>>> import undata as un >>> print(un.powerquery("3.1.1", "Southern Asia", years=(2015, 2023)))