Vault #6: Open Policing Project
In this DISCOVERY Data Science Project, you will do real data science in less than an hour and you will earn this project's card to your collection when you fully complete this project! 🎉
Data Source: Stanford Open Policing Project
The Stanford Open Policing Project is an initiative by the Stanford Computational Journalism Lab to collect, analyze, and provide access to traffic stop data from across the United States. According to the Open Policing Project, "on a typical day in the United States, police officers make more than 50,000 traffic stops". The Open Policing Project aims to provide a comprehensive view of interactions between police officers and the public, focusing on the data collected during traffic stops. It's a crucial resource for understanding issues related to racial profiling, law enforcement tactics, and social justice.
The data is collected from state-level and local-level police departments across the country. As of 2023, the project has already collected more than 200 million records from 42 state police departments and growing. The data is collected in a standardized format, and the project provides a data dictionary to explain the meaning of each column in the data.
This Project will only be looking at the data from the state of Illinois and explore one possible racial disparity among traffic stops in Champaign-Urbana.
Getting Started: Importing the Necessary Libraries
To work with the data, we'll need to import pandas:
Part 1: Data Preparation
To get the data ready for analysis we will first need to do some preprocessing.
Puzzle 1.1: Read The Data
The full data for the state of Illinois is quite large: the CSV is over 2 GB, has 12,748,173 rows (one for every traffic stop), and contains 21 columns. That file is too large to load here, so stanford-open-policing-champaign-urbana-data.csv.xz instead contains a much smaller extract of just the 102,891 traffic stops made by police departments in Champaign and Urbana -- the stops we will analyze in this Project.
Unlike the CSV files you have used so far, which store data as plain text (one row per line), stanford-open-policing-champaign-urbana-data.csv.xz is a compressed (XZ) file -- a compressed container that holds a file squeezed down to a fraction of its original size (our extract is about 17 MB as a plain-text CSV, but less than 1 MB when zipped). That won't cause any problems, as pandas can read a compressed file that contains a CSV inside using pd.read_csv.
Use pd.read_csv to read stanford-open-policing-champaign-urbana-data.csv.xz into a DataFrame called df:
⚙️ Test Case: Puzzle 1.1: Read The Data
Puzzle 1.2: Examine the Data
Before we start working with the data, let's see what columns are available. Because the data is so large, we don't want to print out the entire DataFrame. Instead we can use the columns attribute to get a list of all the columns in the DataFrame.
Now that we know what columns are available, let's look at all the unique values in one of the columns. We can use the unique method to get a list of all the unique values in a column. Let's look at the unique values in the department_name column. The department_name column contains the name of the police department that made the traffic stop.
The full Illinois dataset contains stops from over 1000 different police departments. Our extract only includes the four departments in Champaign and Urbana, but we've included a second file, illinois-departments.csv, that summarizes the full statewide dataset: every one of the 1,013 departments and how many of the 12,748,173 stops each department made.
Let's load that summary and take a look:
Puzzle 1.3: Keeping Relevant Rows
Our df contains only stops made in the Champaign-Urbana area. To make sure we are ready for analysis, we will select just the data from the cities of Champaign and Urbana. We will use thedepartment_name column to filter the data. We will want to see if either the word "Champaign" OR "Urbana" appears in the department_name column.
- Using the
.str.containsoperation on a DataFrame, create a DataFrame calledUIUC_dfthat contains only the rows where thedepartment_namecontains "Champaign" or "Urbana". - See the DISCOVERY guide Selecting DataFrame Rows Based on String Contents for quick details on how to use
.str.contains. - Make sure to check the Puzzle 1.2 to notice what case the strings need to be in to find a match.
⚙️ Test Case: Puzzle 1.3: Keeping Relevant Rows
Part 2: Analyzing Racial Disparities among Chambana Traffic Stops
In Part 2 we will look at how likely it is for different racial groups to be searched during a traffic stop in Champaign-Urbana (or simply "Chambana").
In this Project, we're going to specifically explore:
- The number of total traffic stops by race,
- The number of traffic stops that result in a search taking place by race, and finally
- The proportion of all traffic stops that result in a search taking place by race,
You can go further and analyze many other things in the dataset as well!
Puzzle 2.1: Frequency of Stops per Race
To find the frequency of stops per race, we will focus on the subject_race column in the dataset. This column categorizes each traffic stop by the race of the person who was stopped. Since the UIUC_df dataset already contains all the stops in the Chambana area, we can use the subject_race column to get a comprehensive view of racial distributions in traffic stops in the Champaign-Urbana area.
Using groupby (see: DISCOVERY: Grouping Data in Python for a refresher), group the data by the subject_race columns and aggregate the cells together by counting the total number of rows for each subject_race.
Store this new DataFrame containing the counts of traffic stops by race as traffic_stops_by_race:
Finally, modify your traffic_stops_by_race DataFrame so that it has only two columns: subject_race and stops:
(There is no column named stops so you will have to create one)
Puzzle 2.2: Frequency of Searches per Race
Repeat the process again, except now create a DataFrame traffic_stops_searches_by_race.
- In this new DataFrame, make sure to only include rows from
UIUC_dfwheresearch_conductedisTrue. - Then, ensure
traffic_stops_searches_by_racecontains only the columnssubject_raceandsearches. (Make thesearchescolumn in a similar way that you made thestopscolumn above) - Feel free to create extra Python cells here if needed. :)
⚙️ Test Case: Puzzles 2.1 and 2.2: Creating Filtered DataFrames
Puzzle 2.3: Likelihood of Being Searched by Race
Now that we have found the frequency of certain races being stopped and searched we want to find the probability of a person who has been stopped to be searched given their race. More specifically, we want to find:
$ P(searched | RACE = race) $
...where race is a race from the subject_race column.
We will do this by simply dividing the number of people who have been searched by the number of people who have been stopped for each race. The first thing we need to do is to combine the two DataFrames.
- Since our DataFrames
traffic_stops_by_raceandtraffic_stops_searches_by_raceboth have a shared column name, we can usepd.mergeto merge the two DataFrames. - See the DISCOVERY Guide: Combining DataFrames by Merging to see the specific syntax.
Create a new DataFrame called df_merged that merges the two DataFrames into a single DataFrame:
Finally, create a new column in df_merged called "P(searched | race)" that contains the probability of being searched for each race:
⚙️ Test Case: Puzzle 2.3: Likelihood of Being Searched by Race
Part 3: Visualizing the Disparity
Using df_merged.plot.bar(...), we can visualize our DataFrame to see the disparity:
Validate and Complete This Project!
Congratulations on finishing this DISCOVERY Data Science Project! 🎉🎉
To validate your entire project, your entire code will run from top-to-bottom on this page and each test case will be validated one final time. If everything looks good, you'll earn the card for completing this project: