从多级索引的 Series 中选择数据
对多个列执行一次 .groupby() 操作的输出是带有多级索引(MultiIndex)的 Series。处理这种对象与处理 DataFrame 类似:
- 外层索引相当于 DataFrame 的行。
- 内层索引相当于 DataFrame 的列。
在本练习中,您将使用 .loc[] 访问器来练习从多级索引的 Series 中读取数据。
本练习是课程的一部分
使用 pandas 分析警务活动
练习说明
- 将上一个练习中
.groupby()操作的输出保存为新对象arrest_rate。(已为您完成。) - 打印
arrest_rateSeries 并查看其结构。 - 打印坏天气下因移动违规的逮捕率。
- 打印在三种天气条件下因超速违规的逮捕率。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Save the output of the groupby operation from the last exercise
arrest_rate = ri_weather.groupby(['violation', 'rating']).is_arrested.mean()
# Print the 'arrest_rate' Series
print(____)
# Print the arrest rate for moving violations in bad weather
print(____)
# Print the arrest rates for speeding violations in all three weather conditions
print(____)