Does gender affect who gets a ticket for speeding?

Analyzing Police Activity with pandas

Kevin Markham

Founder, Data School

Filtering by multiple conditions (1)

female = ri[ri.driver_gender == 'F']

female.shape
(23774, 13)
Analyzing Police Activity with pandas

Filtering by multiple conditions (2)

female_and_arrested = ri[(ri.driver_gender == 'F') &
                         (ri.is_arrested == True)]
  • Each condition is surrounded by parentheses
  • Ampersand (&) represents the and operator
female_and_arrested.shape
(669, 13)
  • Only includes female drivers who were arrested
Analyzing Police Activity with pandas

Filtering by multiple conditions (3)

female_or_arrested = ri[(ri.driver_gender == 'F') |
                        (ri.is_arrested == True)]
  • Pipe (|) represents the or operator
female_or_arrested.shape
(26183, 13)
  • Includes all females
  • Includes all drivers who were arrested
Analyzing Police Activity with pandas

Rules for filtering by multiple conditions

  • Ampersand (&): only include rows that satisfy both conditions

  • Pipe (|): include rows that satisfy either condition

  • Each condition must be surrounded by parentheses

  • Conditions can check for equality (==), inequality (!=), etc.

  • Can use more than two conditions

Analyzing Police Activity with pandas

Correlation, not causation

  • Analyze the relationship between gender and stop outcome

    • Assess whether there is a correlation
  • Not going to draw any conclusions about causation

    • Would need additional data and expertise
    • Exploring relationships only
Analyzing Police Activity with pandas

Let's practice!

Analyzing Police Activity with pandas

Preparing Video For Download...