Created
November 22, 2021 11:57
-
-
Save spoonerf/e2d753ab7fd28066983c902f9cd5e060 to your computer and use it in GitHub Desktop.
Download UN SDG indicators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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