Get Started

Calculate Proportions

Nationally, 55% of Hispanics identify as White and 35% identify as "Some Other Race". (You can run Line 2 in the code window to confirm this.) But there is substantial state-to-state variation, which we will now investigate. As a reminder, we will express proportions as percentages throughout this course.

pandas has been imported, the DataFrame states is loaded with population counts by race and Hispanic origin. A list, hispanic_races, has names of columns with Hispanics by race data, and is shown in the console.

This is a part of the course

“Analyzing US Census Data in Python”

View Course

Exercise instructions

  • Use the copy method to create a deep copy of only the hispanic_races columns in states
  • As you iterate the races in the hispanic_races list, calculate the percentage of Hispanics identifying as each race as 100 times the count of the current race divided by the total number of Hispanics.
  • Print the head of the resulting DataFrame.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# What percentage of Hispanics identify as each race?
print(100 * states[hispanic_races].sum() / states["hispanic"].sum())

# Create a deep copy of only the Hispanic race columns
states_hr = ____.copy()

# Calculate percentages for all columns in the date frame
for race in hispanic_races:
    states_hr[race] = ____ * ____ / states["hispanic"]

# View the result
print(____)

This exercise is part of the course

Analyzing US Census Data in Python

IntermediateSkill Level
0.0+
0 reviews

Learn to use the Census API to work with demographic and socioeconomic data.

Start exploring Census data products with the Decennial Census. Use the Census API and the requests package to retrieve data, load into pandas data frames, and conduct exploratory visualization in seaborn. Learn about important Census geographies, including states, counties, and tracts.

Exercise 1: Census Subject TablesExercise 2: Aggregate and Calculate ProportionsExercise 3: Calculate Proportions
Exercise 4: Identify Extreme ValuesExercise 5: Using the Census APIExercise 6: The Basic API RequestExercise 7: The API Response and PandasExercise 8: API to Visualization: Group QuartersExercise 9: Census GeographiesExercise 10: Specific PlacesExercise 11: Congressional Districts by StateExercise 12: Zip Code Tabulation Areas

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free