開始使用免費開始

從多重索引的 Series 取值

對多個欄位做一次 .groupby() 之後會得到具有 MultiIndex 的 Series。操作這類物件的方式與 DataFrame 類似:

  • 外層索引就像 DataFrame 的列。
  • 內層索引就像 DataFrame 的欄。

在這個練習中,你會用 .loc[] 存取器來練習從多重索引的 Series 存取資料。

本練習屬於課程

使用 pandas 分析警方執法活動

檢視課程

練習說明

  • 將上一題 .groupby() 的輸出存成新物件 arrest_rate。(已為你完成。)
  • 列印 arrest_rate Series 並檢視內容。
  • 列印在惡劣天氣下,移動違規(moving violations)的逮捕率。
  • 列印超速違規(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(____)
編輯並執行程式碼