Get startedGet started for free

Congressional Districts by State

A Pennsylvania-based family advocacy nonprofit wants to find the average family size by Congressional district, so that they can call representatives from districts with relatively large families. For this exercise you will have to know the GEOID (ANSI Code) for Pennsylvania. If you don't remember it from a previous exercise, use the Geographic Codes Lookup at the Missouri Census Data Center.

The requests and pandas (as pd) 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

View Course

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[____] = ____
r = requests.get(base_url, params=predicates)

# Construct the DataFrame
col_names = ["name", "avg_family_size", "state", "cd"]
cd = pd.DataFrame(columns=col_names, data=r.json()[1:])
Edit and Run Code