Analyzing US Census Data in Python
Lee Hachadoorian
Asst. Professor of Instruction, Temple University
import requests HOST = "https://api.census.gov/data" year = "2010" dataset = "dec/sf1" base_url = "/".join([HOST, year, dataset]) predicates = {} predicates["get"] = "NAME,P001001"
predicates["for"] = "state:*"
r = requests.get(base_url, params=predicates)
import requests HOST = "https://api.census.gov/data" year = "2010" dataset = "dec/sf1" base_url = "/".join([HOST, year, dataset]) predicates = {} predicates["get"] = "NAME,P001001"
predicates["for"] = "state:42"
r = requests.get(base_url, params=predicates)
Legal/Administrative
Statistical
Request all counties in specific states:
predicates["for"] = "county:*"
predicates["in"] = "state:33,50"
Request specific counties in one state:
predicates["for"] = "county:001,003"
predicates["in"] = "state:33"
r = requests.get(base_url, params=predicates)
Source: https://www.census.gov/geo/reference/gtc/gtc_place.html
Geography Level | Geography Hierarchy |
---|---|
40 | state |
50 | state› county |
60 | state› county› county subdivision |
101 | state› county› tract› block |
140 | state› county› tract |
150 | state› county› tract› block group |
160 | state› place |
state› congressional district› county (or part)
predicates = {} predicates["get"] = "NAME,P001001"
predicates["for"] = "county (or part):*"
predicates["in"] = "state:42;congressional district:02"
r = requests.get(base_url, params=predicates) print(r.text)
[["NAME","P001001","state","congressional district","county"],
["Montgomery County (part)","36793","42","02","091"],
["Philadelphia County (part)","593484","42","02","101"]]
Analyzing US Census Data in Python