Skip to content

Instantly share code, notes, and snippets.

@spoonerf
Created November 22, 2021 11:57
Show Gist options
  • Save spoonerf/e2d753ab7fd28066983c902f9cd5e060 to your computer and use it in GitHub Desktop.
Save spoonerf/e2d753ab7fd28066983c902f9cd5e060 to your computer and use it in GitHub Desktop.
Download UN SDG indicators
import requests
import json
import pandas as pd
from io import BytesIO
base_url = "https://unstats.un.org/sdgapi"
url = f"{base_url}/v1/sdg/Goal/List"
res = requests.get(url)
assert res.ok
goals = json.loads(res.content)
goal_codes = [int(goal["code"]) for goal in goals]
# retrieves all area codes
print("Retrieving area codes...")
url = f"{base_url}/v1/sdg/GeoArea/List"
res = requests.get(url)
assert res.ok
areas = json.loads(res.content)
area_codes = [int(area["geoAreaCode"]) for area in areas]
# retrieves csv with data for all codes and areas
print("Retrieving data...")
url = f"{base_url}/v1/sdg/Goal/DataCSV"
res = requests.post(url, data={"goal": goal_codes, "areaCodes": area_codes})
assert res.ok
df = pd.read_csv(BytesIO(res.content), low_memory=False)
df.to_csv("UN_SDG_2021.zip", index=False, compression="gzip")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment