Zip Code Tabulation Areas
In the marketing field, it is very common to want to know ZIP Code demographics. ZIP Code Tabulation Areas ("ZCTAs") are Census-defined equivalents to ZIP Codes that are built out of Census blocks. In this exercise you will request total population for all ZCTAs in the state of Alabama.
In pandas, an index can be used to retrieve particular rows. The GEOIDs are suitable row identifiers. In this exercise you will set a multilevel index based on the state and ZCTA of each row.
The requests and pandas packages have been imported. The base_url is defined, as is the predicates dictionary with the list of variables to request.
This exercise is part of the course
Analyzing US Census Data in Python
Exercise instructions
- Set the
"for"key in thepredicatesdict to return all ZCTAs; spell out the geography in full as"zip code tabulation area (or part)"" - Set the
"in"key in thepredicatesdict to only return ZCTAs in the state of Alabama; you will have to look up the FIPS code for Alabama in one of the online sources you have learned about - Set the DataFrame index to be the concatenation of the
stateandzctacolumns. Useinplace = Trueto not create a new DataFrame.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Build dictionary of predicates and execute the request
predicates = {}
predicates["get"] = ",".join(["NAME", "P001001"])
____
____
r = requests.get(base_url, params=predicates)
# Construct the DataFrame
col_names = ["name", "total_pop", "state", "zcta"]
zctas = pd.DataFrame(columns=col_names, data=r.json()[1:])
zctas["total_pop"] = zctas["total_pop"].astype(int)
# Set multilevel index from GEOIDs and print the head
zctas.set_index([____, ____], inplace = True)
print(zctas.head())