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.
This exercise is part of the course
Analyzing US Census Data in Python
Exercise instructions
- Create
tidy_white_empby restrictingmsa_white_empto the columns"msa"and"pct_male_unemp", then rename the second column to"white" - Merge
tidy_black_empandtidy_white_empon the"msa"column; assign totidy_emp - Use
meltontidy_emp. Thevalue_varsshould be the names of the two race columns; setvar_nameto"race"andvalue_nameto"unemployment" - Plot unemployment vs. dissimilarity, conditioning on race using the
hueparameter
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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()