开始使用免费开始使用

将停车时长转换为数字

在交通拦截数据集中,stop_duration 列表示警官大致拦截驾驶员的时长。遗憾的是,这些时长以字符串形式存储,如 '0-15 Min'。怎样才能让这些数据更易于分析?

在本练习中,您将把停车时长转换为整数。由于没有精确时长,您需要用合理的取值来估算:

  • '0-15 Min' 转换为 8
  • '16-30 Min' 转换为 23
  • '30+ Min' 转换为 45

本练习是课程的一部分

使用 pandas 分析警务活动

查看课程

练习说明

  • 打印 stop_duration 列中的唯一值。(此步骤已为您完成。)
  • 创建名为 mapping 的字典,将 stop_duration 的字符串映射到题目给定的整数。
  • 使用该 mappingstop_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(____)
编辑并运行代码