開始使用免費開始

複習你的操作

太棒了!你已經學會了樞紐分析表。上一個分析中,你遇到了一個具有非唯一索引/欄位配對的 DataFrame。為了能進行樞紐轉換,你寫了程式碼來刪除最後一列,然後再進行重塑。

在這個練習中,你會改用樞紐分析表來修改程式碼,並將結果與你使用 pivot 方法的策略做比較。

fifa_players 資料集已為你準備好。

本練習屬於課程

使用 pandas 重塑資料

檢視課程

練習說明

  • 捨棄 fifa_players DataFrame 的第 5 列。
  • fifa_players 上使用 .pivot(),將所有分數以 name 作為索引,並以欄位中的 movement 來區分。
  • 使用樞紐分析表,依 namemovement 顯示所有分數的平均值,並將 name 設為索引。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Discard the fifth row to delete all repeated rows
fifa_drop = fifa_players.____(____)

# Use pivot method to get all scores by name and movement
fifa_pivot = fifa_drop.____(____) 

# Print fifa_pivot
print(fifa_pivot)  

# Use pivot table to get all scores by name and movement
fifa_pivot_table = fifa_players.____(index=____, 
                                     columns=____, 
                                     aggfunc=____)
# Print fifa_pivot_table
print(fifa_pivot_table)
編輯並執行程式碼