LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Analyzing US Census Data in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

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

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