Unemployment
Unemployment varies by race and sex. In this exercise, you will start with a DataFrame, unemp_by_race
, of percent unemployment by year for 25 to 54 year olds in four racial groups (White, Black, Asian, and Hispanic) and both sexes. You will create a bar plot of percent unemployment against year.
Because the column names will, after melting, become labels in your final plot, begin by providing column names that are shorter and clearer. The necessary code is provided at the beginning of the exercise.
pandas
and seaborn
have been imported with the usual aliases. unemp_by_race
is loaded, and the dict you will use for renaming is displayed in the console.
This exercise is part of the course
Analyzing US Census Data in Python
Exercise instructions
melt
theunemp_by_race
DataFrame; setid_vars
to"year"
, and remove thevalue_vars
parameter to use all remaining columns as value columns- Create a bar plot of
unemp_by_race
, with the year on the x-axis and the percent unemployed on the y-axis, withhue
determined the demographic group
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Rename columns
unemp_by_race.rename(columns = col_rename, inplace = True)
# Melt DataFrame by demographic group
unemp_by_race = unemp_by_race.melt(id_vars = ____, value_vars = ____,
var_name = "demographic", value_name = "pct_unemployed")
# Plot unemployment by group by year
sns.barplot(x = ____, y = ____, hue = ____, data = unemp_by_race)
plt.show()