开始使用免费开始使用

修正数据类型

在上一个练习中,我们看到 is_arrested 列当前的数据类型是 object。本练习中,我们将其改为 bool,这最适合包含 TrueFalse 值的列。

修正数据类型后,您就可以在 is_arrested 列上使用原本无法进行的数学运算。

本练习是课程的一部分

使用 pandas 分析警务活动

查看课程

练习说明

  • 查看 is_arrested 列的前几行,确认其包含 TrueFalse 值,并检查该列的数据类型。
  • 使用 .astype() 方法将 is_arrested 转换为 bool 列。
  • 再次检查 is_arrested 的数据类型,确认它现在是 bool 列。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Examine the head of the 'is_arrested' column
print(ri.is_arrested.____)

# Change the data type of 'is_arrested' to 'bool'
ri['is_arrested'] = ri.is_arrested.____

# Check the data type of 'is_arrested' 
print(____)
编辑并运行代码