LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Analyzing US Census Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Set the "for" key in the predicates dict to return all ZCTAs; spell out the geography in full as "zip code tabulation area (or part)""
  • Set the "in" key in the predicates dict 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 state and zcta columns. Use inplace = True to not create a new DataFrame.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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())
Code bearbeiten und ausführen