WFP topline figures (tabular CSV)¶
Download a small World Food Programme indicator table and read it with pandas. Live query against data.humdata.org (public, no credentials); wrapped so it stays safe under nbval-lax offline.
Setup¶
Import pandas and the unified EarthLens entry point, then prepare a local hdx_output/ directory for the downloaded file.
In [ ]:
Copied!
from pathlib import Path
import pandas as pd
from earthlens import EarthLens
OUT_DIR = Path('hdx_output')
OUT_DIR.mkdir(exist_ok=True)
from pathlib import Path
import pandas as pd
from earthlens import EarthLens
OUT_DIR = Path('hdx_output')
OUT_DIR.mkdir(exist_ok=True)
Download the indicator table¶
HDX is a file-writing backend, so we use download(): it writes the WFP topline CSV into OUT_DIR and returns the list of written paths. We build the request first, then call download() on its own line. The whole live query is wrapped in a try/except so the notebook stays safe offline under nbval-lax.
In [ ]:
Copied!
paths = None
try:
table = EarthLens(
data_source='hdx',
dataset='wfp-topline-figures',
variables=[],
path=str(OUT_DIR),
)
paths = table.download(progress_bar=False)
print('downloaded:', [Path(p).name for p in paths])
except Exception as exc:
print(f'skipped live query: {type(exc).__name__}: {exc}')
paths = None
try:
table = EarthLens(
data_source='hdx',
dataset='wfp-topline-figures',
variables=[],
path=str(OUT_DIR),
)
paths = table.download(progress_bar=False)
print('downloaded:', [Path(p).name for p in paths])
except Exception as exc:
print(f'skipped live query: {type(exc).__name__}: {exc}')
Read it with pandas¶
If the download succeeded, read the first written CSV into a DataFrame and preview its shape and the leading rows.
In [ ]:
Copied!
if paths:
df = pd.read_csv(paths[0])
print('rows, cols:', df.shape)
df.head()
if paths:
df = pd.read_csv(paths[0])
print('rows, cols:', df.shape)
df.head()