Last active
October 4, 2018 11:36
-
-
Save Proteusiq/18bc4add068c5145aa4182dd467596c3 to your computer and use it in GitHub Desktop.
Get Denmark Postal Codes from Dawa as a Pandas’ DataFrame
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
from collections import defaultdict | |
import requests | |
import pandas as pd | |
def get_postal(only_postal=True): | |
''' | |
Simple function that returns DK postal codes from DAWA. If only_post is false, | |
postal code name, kommune, latitude and longitude are returned | |
how to use: | |
df = get_postal(only_postal=False) | |
''' | |
post_data = defaultdict(list) | |
url = 'http://dawa.aws.dk/postnumre' | |
r = requests.get(url) | |
if r.ok: | |
posts = r.json() | |
for _, post in enumerate(posts): | |
post_data['Postal'].append(post['nr']) | |
post_data['Name'].append(post['navn']) | |
post_data['Kommuner'].append(post['kommuner'][0]['navn'] if post['kommuner'] else None) | |
post_data['Longitude'].append(post['visueltcenter'][0]) | |
post_data['Latitude'].append(post['visueltcenter'][1]) | |
post_data['bbox'].append(post['bbox']) | |
if only_postal: | |
return pd.DataFrame(post_data)[['Postal']] | |
else: | |
return pd.DataFrame(post_data) | |
else: | |
print(f'Requests failed: {r}') | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment