将停车时长转换为数字
在交通拦截数据集中,stop_duration 列表示警官大致拦截驾驶员的时长。遗憾的是,这些时长以字符串形式存储,如 '0-15 Min'。怎样才能让这些数据更易于分析?
在本练习中,您将把停车时长转换为整数。由于没有精确时长,您需要用合理的取值来估算:
- 将
'0-15 Min'转换为8 - 将
'16-30 Min'转换为23 - 将
'30+ Min'转换为45
本练习是课程的一部分
使用 pandas 分析警务活动
练习说明
- 打印
stop_duration列中的唯一值。(此步骤已为您完成。) - 创建名为
mapping的字典,将stop_duration的字符串映射到题目给定的整数。 - 使用该
mapping将stop_duration的字符串转换为整数,并将结果存入新列stop_minutes。 - 打印
stop_minutes列中的唯一值,以验证时长已正确转换为整数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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(____)