LoslegenKostenlos loslegen

White and Black Unemployment

In this exercise you will compare metropolitan unemployment between White and Black males. msa_black_emp is loaded. A new DataFrame, msa_white_emp, with data from table C23002A from the 2012 5-year ACS is also loaded. Percentage unemployment has already been calculated for you. You will restrict both DataFrames to the columns of interest (the ones showing percentage male employment), join the DataFrames, and melt them into a tidy DataFrame for visualization with seaborn.

pandas and seaborn have been loaded using the usual aliases.

Diese Übung ist Teil des Kurses

Analyzing US Census Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Create tidy_white_emp by restricting msa_white_emp to the columns "msa" and "pct_male_unemp", then rename the second column to "white"
  • Merge tidy_black_emp and tidy_white_emp on the "msa" column; assign to tidy_emp
  • Use melt on tidy_emp. The value_vars should be the names of the two race columns; set var_name to "race" and value_name to "unemployment"
  • Plot unemployment vs. dissimilarity, conditioning on race using the hue parameter

Interaktive Übung

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

# Restrict DataFrame to columns of interest, rename columns
tidy_black_emp = msa_black_emp[["msa", "D", "pct_male_unemp"]]
tidy_black_emp.columns = ["msa", "D", "black"]
tidy_white_emp = ____
tidy_white_emp.columns = ____
tidy_emp = ____

# Use melt to create tidy DataFrame
tidy_msa_emp = tidy_emp.melt(id_vars = ["msa", "D"], 
    value_vars = ____, var_name = ____, 
    value_name = ____)

# Visually compare male and female unemployment
sns.lmplot(____, data = tidy_msa_emp)
plt.show()
Code bearbeiten und ausführen