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.
Bu egzersiz, kursun bir parçasıdır
Analyzing US Census Data in Python
Egzersiz talimatları
- 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.
Uygulamalı etkileşimli egzersiz
Bu egzersizi bu örnek kodu tamamlayarak deneyin.
# 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())