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_wideexceptyearand ensure that the columns with the months and values are namedmonthandunempl_rate, respectively. Save the result asur_tall. - Add a column to
ur_tallnameddatewhich combines theyearandmonthcolumns as year-month format into a larger string, and converts it to a date data type. - Sort
ur_tallby date and save asur_sorted. - Using
ur_sorted, plotunempl_rateon the y-axis anddateon 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()