GGR274 Homework 4: Using Census Microdata#

Logistics#

Due date: The homework is due 23:59 on Monday, February 2.

You will submit your work on MarkUs. To submit your work:

  1. Download this file (Homework_4.ipynb) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)

  2. Submit this file to MarkUs under the hw4 assignment. (See our MarkUs Guide for detailed instructions.) All homeworks will take place in a Jupyter notebook (like this one). When you are done, you will download this notebook and submit it to MarkUs.

Introduction#

For this week’s homework, you will use data from the Public Use Microdata File (PUMF) from the 2021 Census. We’re going to dig into some of the variables looking at how people commute to work, and how long it takes for them to commute if they use public transit.

Question#

The question you’re answering in this homework:

Among respondents that live in the Toronto metro area, which group is more likely to have a commute of 60+ minutes: public transit riders, or drivers?

Homework Instructions and Learning Objectives#

The goal of this homework is to answer the question above performing these steps:

  • Read the PUMF Dataset into a pandas DataFrame.

  • Select specific columns and rows of the DataFrame.

  • What proportions of respondents in Toronto use public transit to commute to work? What proportion of public transit riders in Toronto take an hour or more to commute?

  • Interpret the results of the analysis.

Task 1#

a) Read the census data stored in the csv file census_2021_pumf_filtered.csv into a pandas DataFrame and store the DataFrame in a variable named pumf_data_raw.

The file is located in the same folder as the notebook.

import pandas as pd

# Write your code here

b) The columns we will need for the analysis to answer the question are:

  • CMA: Census Metropolitan Area

  • MODE: Main mode of commuting to work

  • PWDUR: Amount of time commuting to work (in minutes)

Create a new DataFrame using pumf_data_raw that only contains the three columns listed above. The first column should be CMA, the second column should be MODE, and the third column should be PWDUR.

This new DataFrame should be stored in a variable named pumf_data.

# Write your code here

c) Create a Python dictionary stored in a variable called new_column_names, that maps old column name to new column name according to the following table:

old name

new name

CMA

metro_area

MODE

commute_mode

PWDUR

commute_duration

You’ll use this dictionary to rename the columns in part (d) below.

# Write your code here

d) Use the dictionary new_column_names created in the previous step to rename the columns of the DataFrame stored in pumf_data. Store this new DataFrame in a variable called clean_pumf_data.

# Write your code here

Task 2#

a) Use the PUMF code book in the file 2021 Census Hierarchical PUMF User Guide_V2.pdf to guide you in creating boolean variables using clean_pumf_data that correspond the the following conditions and store the results in the variable names specified below.

Condition

variable name

Commutes to work by taking public transit

transit

Commutes to work by driving (not as a passenger)

driving

Takes 60+ minutes to commute to work

commute_60

Lives in Toronto Census Metropolitan Area

toronto

Tip: Go to File -> Open menu action to find the 2021 Census Hierarchical PUMF User Guide_V2.pdf file in the same folder as this notebook. Search for the old variable names in this PDF to determine these categories!

# Write your code here

b) In this part of the task you will investigate the data types of one of the variables you created in the previous part.

i) Store the data type of transit in a variable called transit_col_type and print the value of transit_col_type.

# Write your code here

ii) Store the data type of values in transit in a variable called transit_data_type and print the value of transit_data_type.

You can access the data type of values in a pd.Series with pd.Series.dtypes.

# Write your code here

c) Briefly explain the difference between the values of transit_col_type and transit_data_type.

Answer Task 2 c) here.

Task 3#

In this section you will write a program in a series of steps to analyse the data.

Use the DataFrame clean_pumf_data and the variables that you created in Task 2 a).

The data analysis will be implemented by writing a Python program to compute two proportions that you will express as percentages (i.e., multiplying by 100).

\[{\text{Percent}_\text{Transit}} = \frac{\text{Number of Toronto respondents that commute by public transit for 60+ minutes}}{\text{Number of Toronto respondents that commute by public transit}}\times 100 \]
\[{\text{Percent}_\text{Driving}} = \frac{\text{Number of Toronto respondents that commute by driving for 60+ minutes}}{\text{Number of Toronto respondents that commute by driving}}\times 100 \]

a) Create a variable called toronto_transit that is True if a respondent lives in the Toronto area and uses public transit.

Then, use this variable to select rows in clean_pumf_data and then compute the number of such rows, storing the result in a variable called toronto_transit_num. This is the value of: \(\text{Number of Toronto respondents that commute by public transit}\).

Finally, print the value of toronto_transit_num.

# Write your code here

b) Create a variable called toronto_transit_60mins that is True if a respondent lives in the Toronto area, uses public transit, and has a commute to work of 60 minutes and over.

Then, use this variable to select rows in clean_pumf_data and then compute the number of such rows, storing the result in a variable called toronto_transit_60mins_num. This is the value of: \(\text{Number of Toronto respondents that commute by public transit for 60+ minutes}\).

Finally, print the value of toronto_transit_60mins_num.

# Write your code here

c) Calculate the proportion of public transit commuters in Toronto who commute for 60+ minutes. Store the result in a variable called toronto_transit_60mins_prop.

# Write your code here

d) Use the print function to print the following sentence:

The share of Toronto area transit riders that commute for 60+ minutes is {XX}%.

Where XX is the value of toronto_transit_60mins_prop multiplied by 100 and rounded to two decimal places. This is the value of: \({\text{Percent}_\text{Transit}}\).

# Write your code here

e) Create a variable called toronto_driving that is True if a respondent lives in the Toronto area and drives to work.

Then, use this variable to select rows in clean_pumf_data and then compute the number of such rows, storing the result in a variable called toronto_driving_num. This is the value of: \(\text{Number of Toronto respondents that commute by driving}\).

Finally, print the value of toronto_driving_num.

# Write your code here

f) Create a variable called toronto_driving_60mins that is True if a respondent lives in the Toronto area, drives to work, and their commute is 60+ minutes long.

Then, use this variable to select rows in clean_pumf_data and then compute the number of such rows, storing the result in a variable called toronto_driving_60mins_num. This is the value of: \(\text{Number of Toronto respondents that commute by driving for 60+ minutes}\).

Finally, print the value of toronto_driving_60mins_num.

# Write your code here

g) Calculate the proportion of Toronto drivers that take 60+ minutes to commute. Store the result in a variable called toronto_driving_60mins_prop.

# Write your code here

h) Use the print function to print the following sentence:

The share of Toronto area drivers that commute for 60+ minutes is {XX}%.

Where XX is the value of toronto_driving_60mins_prop multiplied by 100 and rounded to two decimal places. This is the value of: \({\text{Percent}_\text{Driving}}\).

# Write your code here

Task 4#

Answer the following questions.

a) Is the data analysis above sufficient to answer the original question? If yes then explain why it’s sufficient, otherwise explain what type of analysis would have provided appropriate information to help answer the question. Briefly explain your reasoning.

Answer Task 4 a) here.

b) Does the data analysis you performed above provide evidence that public transit in Toronto is slower on average than driving? If the analysis doesn’t support this claim then describe an analysis that would give you evidence to evaluate this claim. Briefly explain your reasoning.

Answer Task 4 b) here.

Marking Rubric#

Section

0

1

2

3

Computational questions (for each part)

auto test fails

auto test passes

NA

NA

Qualitative questions (for each part)

No answer

The question is answered but no explanation is given

The question is answered but the explanation is irrelevant or not supported

The question is answered and the explanation is supported