Skip to content

Instantly share code, notes, and snippets.

@aernesto
Created June 27, 2021 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aernesto/1742fe18238bd9fc72a11dc35e19fafa to your computer and use it in GitHub Desktop.
Save aernesto/1742fe18238bd9fc72a11dc35e19fafa to your computer and use it in GitHub Desktop.
Getting Census Data from their APIs
# -*- coding: utf-8 -*-
"""
I ran the following code with Python 3.9
The only dependency was the requests library
https://docs.python-requests.org/en/latest/user/quickstart/
The core concepts for the API calls are explained here
https://www.census.gov/data/developers/guidance/api-user-guide.Core_Concepts.html
You will notice that I use a KEY variable in the code that is not defined anywhere.
I removed its literal definition from the code, since this is a private key that I got
by requesting it at this URL:
https://api.census.gov/data/key_signup.html
"""
from pprint import pprint
import requests
#***** setting up variables to find out age-breakdown of foreign-born population in Philadelphia in 2019*****
suffix = 'E'
DEFS = {
'002E': 'Male Total',
'021E': 'Female Total',
}
ages_start = list(range(0, 90, 5)) # [0, 5, 10, ..., 85]
for i, v in enumerate(ages_start):
DEFS[str(i + 3).zfill(3) + suffix] = f'Male age {v}-{v + 4}'
DEFS[str(i + 22).zfill(3) + suffix] = f'Female age {v}-{v + 4}'
# millenial_years = [1981, 1996]
# millenial_ages = [2019 - x for x in millenial_years] # in 2019
# get=NAME,S0101_C01_001E&for=county:101&in=state:42
# S0201_136E Estimate!!PLACE OF BIRTH, CITIZENSHIP STATUS AND YEAR OF ENTRY!!Foreign born!!Foreign born not a U.S. citizen
# S0201_130E Estimate!!PLACE OF BIRTH, CITIZENSHIP STATUS AND YEAR OF ENTRY!!Foreign born
# S0201_139E Estimate!!PLACE OF BIRTH, CITIZENSHIP STATUS AND YEAR OF ENTRY!!Population born outside the United States
# S0201_012E Estimate!!SEX AND AGE!!Total population!!25 to 34 years
#api.census.gov/data/2019/acs/acs1?get=NAME,group(B01001)&for=us:1&key=YOUR_KEY_GOES_HERE
su, sp = ('https://api.census.gov/data/2019/acs/acs1', {
'get': 'NAME,group(B05013)',
'for': 'county:101',
'in': 'state:42',
'key': KEY
})
#***** setting up variables for other few stats*****
payload = [
# ('https://api.census.gov/data/2019/acs/acs1/subject', {
# 'get': 'NAME,S0101_C01_001E',
# 'description': 'global count',
# 'for': 'county:101',
# 'in': 'state:42',
# 'key': KEY
# }),
('https://api.census.gov/data/2019/acs/acs1/spp', {
'description': 'global count',
'get': 'NAME,S0201_001E',
'for': 'county:101',
'in': 'state:42',
'key': KEY
}),
# ('https://api.census.gov/data/2019/acs/acs1/spp', {
# 'description': """
# Estimate!!LANGUAGE SPOKEN AT HOME AND ABILITY TO SPEAK ENGLISH!!Population 5 years and over!!Language other than English!!Speak English less than "very well"
# """,
# 'get': 'NAME,S0201_153E',
# 'for': 'county:101',
# 'in': 'state:42',
# 'key': KEY
# }),
# ('https://api.census.gov/data/2019/acs/acs1/spp', {
# 'description': """
# Estimate!!PLACE OF BIRTH, CITIZENSHIP STATUS AND YEAR OF ENTRY!!Foreign born!!Foreign born; not a U.S. citizen
# """,
# 'get': 'NAME,S0201_136E',
# 'for': 'county:101',
# 'in': 'state:42',
# 'key': KEY
# }),
# ('https://api.census.gov/data/2019/acs/acs1/spp', {
# 'description': """
# Estimate!!PLACE OF BIRTH, CITIZENSHIP STATUS AND YEAR OF ENTRY!!Foreign born
# """,
# 'get': 'NAME,S0201_130E',
# 'for': 'county:101',
# 'in': 'state:42',
# 'key': KEY
# }),
('https://api.census.gov/data/2019/acs/acs1/spp', {
'description': """
Estimate!!PLACE OF BIRTH, CITIZENSHIP STATUS AND YEAR OF ENTRY!!Population born outside the United States
Annotation
""",
'get': 'NAME,S0201_139E,S0201_139EA',
'for': 'county:101',
'in': 'state:42',
'key': KEY
}),
('https://api.census.gov/data/2019/acs/acs1/spp', {
'description': """
Estimate!!SEX AND AGE!!Total population!!25 to 34 years
""",
'get': 'NAME,S0201_012E',
'for': 'county:101',
'in': 'state:42',
'key': KEY
}),
# ('https://api.census.gov/data/2019/pep/charagegroups', {
# 'description': """
# All AGEGROUPS
# """,
# 'get': "NAME,POP",
# 'for': 'county:101',
# 'in': 'state:42',
# 'AGEGROUP': '0:130',
# 'key': KEY
# }),
('https://api.census.gov/data/2019/acs/acs1/spp', {
'description': """
TWO ABOVE COMBINED?
""",
'get': 'NAME,S0201_012E,S0201_139E',
'for': 'county:101',
'in': 'state:42',
'key': KEY
}),
]
if __name__ == "__main__":
# FIRST CALL FOR AGE BREAKDOWN OF FOREIGN BORN POPULATION OF PHILADELPHIA
req = requests.get(su, params=sp)
if req.status_code != 200:
print(req.url)
print(req.status_code)
pprint(sp)
print(req.text)
raise ValueError()
data = req.json()
print('special results')
pprint(DEFS)
to_display = {k: [] for k in data[0]}
for data_rows in data[1:]:
for ix, value in enumerate(data_rows):
t = data[0][ix]
to_display[t].append(value)
pprint(to_display)
# LIST OF CALLS
for u, p in payload:
print('---------------------------')
print(p.pop('description'))
r = requests.get(u, params=p)
if r.status_code != 200:
raise ValueError()
# print(r.url)
# print(r.status_code)
data = r.json()
title_row = data[0]
to_display = {k: [] for k in title_row}
for data_rows in data[1:]:
for ix, value in enumerate(data_rows):
to_display[title_row[ix]].append(value)
pprint(to_display)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment