Skip to content

Instantly share code, notes, and snippets.

@anil-kk
Last active November 3, 2020 15:00
Show Gist options
  • Save anil-kk/c5e007ba0df938121f5fa4e0da43ce16 to your computer and use it in GitHub Desktop.
Save anil-kk/c5e007ba0df938121f5fa4e0da43ce16 to your computer and use it in GitHub Desktop.
Create lookup<region_code,name> for regions in Sweden from SCB data
import pandas as pd
import requests
import json
session = requests.Session()
res = session.get("http://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningNy")
region_variables = json.loads(res.content.decode('utf-8-sig'))['variables']
regions_lookup = {}
for variable in region_variables:
regions_lookup[variable['text']] = dict(zip(variable['values'], variable['valueTexts']))
regions_lookup['region']
regions_df=pd.DataFrame.from_dict({'code':list(regions_lookup['region'].keys()),'name':list(regions_lookup['region'].values())})
regions_df.to_csv('region_lookup.csv',index=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment