重塑逮捕率資料
在這個練習中,你會先把 arrest_rate 這個 Series 重塑為 DataFrame。處理多重索引的 Series 時,這一步很實用,因為它能讓你使用完整的 DataFrame 方法。
接著,你會用樞紐分析表建立完全相同的 DataFrame。這很好地示範了 pandas 常常有不只一種方式可以得到同樣的結果!
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 將
arrest_rate這個 Series 進行 unstack,以重塑為 DataFrame。 - 使用樞紐分析表建立完全相同的 DataFrame!三個
.pivot_table()的參數都應分別指定為ri_weather的其中一個欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Unstack the 'arrest_rate' Series into a DataFrame
print(arrest_rate.____)
# Create the same DataFrame using a pivot table
print(ri_weather.pivot_table(index='____', columns='____', values='is_arrested'))