Using .melt() to reshape government data
The US Bureau of Labor Statistics (BLS) often provides data series in an easy-to-read format - it has a separate column for each month, and each year is a different row. Unfortunately, this wide format makes it difficult to plot this information over time. In this exercise, you will reshape a table of US unemployment rate data from the BLS into a form you can plot using .melt()
. You will need to add a date column to the table and sort by it to plot the data correctly.
The unemployment rate data has been loaded for you in a table called ur_wide
. You are encouraged to explore this table before beginning the exercise.
This exercise is part of the course
Joining Data with pandas
Exercise instructions
- Use
.melt()
to unpivot all of the columns ofur_wide
exceptyear
and ensure that the columns with the months and values are namedmonth
andunempl_rate
, respectively. Save the result asur_tall
. - Add a column to
ur_tall
nameddate
which combines theyear
andmonth
columns as year-month format into a larger string, and converts it to a date data type. - Sort
ur_tall
by date and save asur_sorted
. - Using
ur_sorted
, plotunempl_rate
on the y-axis anddate
on the x-axis.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# unpivot everything besides the year column
ur_tall = ____
# Create a date column using the month and year columns of ur_tall
ur_tall['date'] = pd.to_datetime(ur_tall['____'] + '-' + ____)
# Sort ur_tall by date in ascending order
ur_sorted = ____
# Plot the unempl_rate by date
ur_sorted.plot(____)
plt.show()