删除列
在实际分析中,DataFrame 往往会包含对您的分析没有用的列。为了更专注于其余列,应该将这些列从 DataFrame 中删除。
在本练习中,您将删除county_name列,因为它只包含缺失值;还会删除state列,因为所有交通拦截都发生在同一个州(罗德岛)。因此,这两列不包含有用信息,可以删除。每列的缺失值数量已经为您打印出来。
本练习是课程的一部分
使用 pandas 分析警务活动
练习说明
- 查看 DataFrame 的
.shape,以了解行数和列数。 - 通过将列名作为字符串列表传给
.drop()方法,同时删除county_name和state两列。 - 再次查看
.shape,确认列数减少了 2。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Examine the shape of the DataFrame
print(ri.____)
# Drop the 'county_name' and 'state' columns
ri.____([____, ____], axis='columns', inplace=True)
# Examine the shape of the DataFrame (again)
print(____)