Worksheet 7
Contents
Worksheet 7#
This worksheet is due Tuesday of Week 5, before discussion section. You are encouraged to work in groups of up to 3 total students, but each student should make their own submission on Canvas. (It’s fine for everyone in the group to have the same upload.)
Overall question#
From 2002 to 2008, how did the unemployment rate change across different industries?
For each of 14 industries, we will write one sentence briefly addressing this question.
Part 1 - Preparing the data#
Load the attached unemployment.csv file as a pandas DataFrame
df
.Convert the “date” column to a datetime dtype, by using
pd.to_datetime
. (Make sure you’ve actually replaced the “date” column, as opposed to displaying a new Series and leavingdf
the same.)Add a “year” column to
df
using thedt
accessor.
Part 2 - Using df.groupby
and a for loop#
(There are other solutions possible, but the point of this worksheet is to practice iterating over a pandas GroupBy object.)
There are 14 total industries in the dataset. Here are two examples of the sentences we want to produce. Notice how in one of the sentences we use the word “increased” and in the other sentence we use the word “decreased”. Notice also that each of our numbers shows two decimal places.
From 2002 to 2008, the median unemployment rate in Leisure and hospitality increased by 2.94%, from 8.50 to 8.75.
From 2002 to 2008, the median unemployment rate in Manufacturing decreased by 18.18%, from 6.60 to 5.40.
Take a minute to understand these computations. For example, do you see why we are saying 8.50 to 8.75 is an increase of 2.94%?
Create a length one list called
text_list
containing the names of your group members as a single string, separated by periods.For each of the 14 industries, append the corresponding sentence to
text_list
. Use the following template. Be sure to copy the f-string portion exactly, because the grader is expecting that you used this template. (There should not be any line breaks in the f-string.)
years = [2002, 2008]
text_list = [???"Name 1. Name 2. Name 3"] # Full names of group members here as a single string
for ???, ??? in df.groupby(???):
??? (as many lines as you need)
change = ??? if ??? else ??? # "increased" or "decreased" as appropriate
sentence = f'''From {years[0]} to {years[1]}, the median unemployment rate in {???} {change} by {???:.2%}, from {???:.2f} to {???:.2f}.'''
text_list.append(sentence)
Part 3 - Submitting the text file#
text_list
is currently a list of strings (this list should have length 15). We can make it into a single string, with each original string separated by a line break, using '\n'.join(text_list)
.
Write that single string to a file and then submit the file on Canvas.
with open("worksheet7.txt", "w") as f:
f.write('\n'.join(text_list))
Reminder#
Every group member needs to submit this on Canvas (even if you all submit the same file).
Submission#
Upload the text file you just created to Canvas.
Created in Deepnote