GGR274 Lab 4: Introduction to Data Wrangling, Part 1#
Logistics#
Like last week, our lab grade will be based on attendance and submission of a few small tasks to MarkUs during the lab session (or by 23:59 on Thursday).
Complete the tasks in this Jupyter notebook and submit your completed file to MarkUs. Here are the instructions for submitting to MarkUs (same as last week):
Download this file (
Lab_4.ipynb
) from JupyterHub. (See our JupyterHub Guide for detailed instructions.)Submit this file to MarkUs under the lab4 assignment. (See our MarkUs Guide for detailed instructions.)
Note: You can use autotests with this week’s lab to see if you are on the right track. Your TA and instructors can look in more detail to see if you answered all questions. The submission is a part of your Lab attendance grade.
Task 1: Read the csv file into a DataFrame
#
a. Read the file#
Read the csv file ArrestsStripSearches.csv
into a pandas Dataframe called police_df
. The file contains records related to Toronto Police Race and Identity Based Data - Arrests and Strip Searches. Inspect the data with .head()
.
The file is located in the same folder as the notebook.
import pandas as pd
# Write your code here
b. Check the size#
Use
.shape
to extract the the size of the table, (number of rows, number of columns).Store the size to a variable named
police_df_size
.
# Write your code here
Task 2: Create a DataFrame
of arrests in 2021#
a. Subset the tabular data by rows#
Create a boolean variable named
arrests_2021
computed frompolice_df
that isTrue
ifArrest_Year
is2021
andFalse
otherwise.Use
Arrests_2021
to select rows ofpolice_df
that correspond to arrests in 2021. Save this new DataFrame in a variable calledpolice_2021_df
. Examine thehead()
of thisDataFrame
.
# Write your code here
b. Check the size#
Use
.shape[0]
to extract the number of rows of the subset,police_2021_df
.Store the arrest counts to a variable named
arrest_counts_2021
.
# Write your code here
c. Inspect columns of the tabular data#
Create a variable called
arrest_column_names
that stores a list of the column names ofpolice_2021_df
and print the list.
# Write your code here
Task 3: Select columns from police_2021_df
and examine the distributions of each column#
Create a new DataFrame from
police_2021_df
with the following columns:_id
,Sex
,Perceived_Race
,SearchReason_PossessWeapons
and assign it to the variablepolice_2021_raceweapons
. Examine thehead()
of thisDataFrame
.Use
.value_counts()
to compute the distributions ofSex
,Perceived_Race
,SearchReason_PossessWeapons
. You do not need to save these values in variables, though you may do so if you want.
# Write your code here