Converting stop durations to numbers
In the traffic stops dataset, the stop_duration
column tells you approximately how long the driver was detained by the officer. Unfortunately, the durations are stored as strings, such as '0-15 Min'
. How can you make this data easier to analyze?
In this exercise, you'll convert the stop durations to integers. Because the precise durations are not available, you'll have to estimate the numbers using reasonable values:
- Convert
'0-15 Min'
to8
- Convert
'16-30 Min'
to23
- Convert
'30+ Min'
to45
This exercise is part of the course
Analyzing Police Activity with pandas
Exercise instructions
- Print the unique values in the
stop_duration
column. (This has been done for you.) - Create a dictionary called
mapping
that maps thestop_duration
strings to the specified integers. - Convert the
stop_duration
strings to integers using themapping
, and store the results in a new column calledstop_minutes
. - Print the unique values in the
stop_minutes
column, to verify that the durations were properly converted to integers.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print the unique values in 'stop_duration'
print(ri.stop_duration.unique())
# Create a dictionary that maps strings to integers
mapping = {____}
# Convert the 'stop_duration' strings to integers using the 'mapping'
ri['stop_minutes'] = ri.stop_duration.____
# Print the unique values in 'stop_minutes'
print(____)