マルチインデックス付き Series からの選択
複数の列に対して .groupby() を1回実行すると、マルチインデックスを持つ Series が得られます。このタイプのオブジェクトの操作は、DataFrame の操作とよく似ています。
- 外側のインデックスレベルは DataFrame の行に相当します。
- 内側のインデックスレベルは DataFrame の列に相当します。
この演習では、.loc[] アクセサを使ってマルチインデックス付き Series からデータにアクセスする練習をします。
この演習はコースの一部です
pandasで警察活動を分析する
演習の手順
- 前の演習で行った
.groupby()の出力を新しいオブジェクトarrest_rateとして保存します。(これはすでに行われています。) arrest_rateSeries を表示して、内容を確認します。- 悪天候におけるmoving violationsの逮捕率を表示します。
- 3つの天候条件すべてにおけるspeeding violationsの逮捕率を表示します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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(____)