修正数据类型
在上一个练习中,我们看到 is_arrested 列当前的数据类型是 object。本练习中,我们将其改为 bool,这最适合包含 True 和 False 值的列。
修正数据类型后,您就可以在 is_arrested 列上使用原本无法进行的数学运算。
本练习是课程的一部分
使用 pandas 分析警务活动
练习说明
- 查看
is_arrested列的前几行,确认其包含True和False值,并检查该列的数据类型。 - 使用
.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(____)