樞紐分析表

使用 pandas 重塑資料

Maria Eugenia Inzaugarat

Data Scientist

pivot 方法的限制

another_fifa.head()
                 name    variable  metric_system  imperial_system
0   Cristiano Ronaldo      weight             83           183.00
1            J. Oblak      weight             87           191.00
2   Cristiano Ronaldo      height            187             6.13
3     J. Oblak             height            188             6.16
4   Cristiano Ronaldo      height            187             6.14
another_fifa.pivot(index="name", columns="variable")
Traceback (most recent call last):
   ValueError: Index contains duplicate entries, cannot reshape
使用 pandas 重塑資料

pivot 方法的限制

  • 通用轉寬工具
  • 索引/欄位配對必須唯一
  • 不能彙總數值
使用 pandas 重塑資料

Pivot table(樞紐分析)

  • 一個含統計摘要的 DataFrame,用來概括更大 DataFrame 的資料

 

顯示摘要統計的 DataFrame

使用 pandas 重塑資料

Pivot table(樞紐分析)

長格式的 DataFrame

使用 pandas 重塑資料

Pivot table(樞紐分析)

箭頭從長格式指向含摘要的寬格式

 

呼叫 pivot_table 的範例

使用 pandas 重塑資料

Pivot table(樞紐分析)

長格式與摘要 DataFrame,標示索引與欄位

 

含 index 與 columns 參數的 pivot 呼叫

使用 pandas 重塑資料

Pivot table(樞紐分析)

長格式與摘要 DataFrame,標示欄與值欄

 

含 values 與彙總函式參數的 pivot 呼叫

使用 pandas 重塑資料

Pivot table(樞紐分析)

another_fifa.pivot_table(index="name", columns="variable", aggfunc="mean")
                     metric_system     imperial_system       
         variable   height  weight     height   weight
             name                                                         
Cristiano Ronaldo      187      83      6.135    183.0
         J. Oblak      188      87      6.160    191.0
使用 pandas 重塑資料

階層式索引(MultiIndex)

fifa_players.head(6)
      first     last   movement   overall  attacking
0    Lionel    Messi   shooting        92         70
1 Cristiano  Ronaldo   shooting        93         89
2    Lionel    Messi    passing        92         92
3 Cristiano  Ronaldo    passing        82         83
4    Lionel    Messi    passing        96         88
5 Cristiano  Ronaldo    passing        89         84
使用 pandas 重塑資料

階層式索引(MultiIndex)

fifa_players.head(6)
      first     last   movement   overall  attacking
0    Lionel    Messi   shooting        92         70
1 Cristiano  Ronaldo   shooting        93         89
2    Lionel    Messi    passing        92         92
3 Cristiano  Ronaldo    passing        82         83
4    Lionel    Messi    passing        96         88
5 Cristiano  Ronaldo    passing        89         84
fifa_players.pivot_table(index=                 , columns="movement", values=                        , aggfunc=     )
使用 pandas 重塑資料

階層式索引(MultiIndex)

fifa_players.head(6)
      first     last   movement   overall  attacking
0    Lionel    Messi   shooting        92         70
1 Cristiano  Ronaldo   shooting        93         89
2    Lionel    Messi    passing        92         92
3 Cristiano  Ronaldo    passing        82         83
4    Lionel    Messi    passing        96         88
5 Cristiano  Ronaldo    passing        89         84
fifa_players.pivot_table(index=["first", "last"], columns="movement", values=                        , aggfunc=     )
使用 pandas 重塑資料

階層式索引(MultiIndex)

fifa_players.head(6)
      first     last   movement   overall  attacking
0    Lionel    Messi   shooting        92         70
1 Cristiano  Ronaldo   shooting        93         89
2    Lionel    Messi    passing        92         92
3 Cristiano  Ronaldo    passing        82         83
4    Lionel    Messi    passing        96         88
5 Cristiano  Ronaldo    passing        89         84
fifa_players.pivot_table(index=["first", "last"], columns="movement", values=["overall", "attacking"], aggfunc="max")
                           attacking           overall
          movement  passing shooting  passing shooting
    first     last                
Cristiano  Ronaldo       84       89       89       93
   Lionel    Messi       92       70       96       92
使用 pandas 重塑資料

總計欄列(margins)

fifa_players.pivot_table(index=["first", "last"], columns="movement", aggfunc="count",             )
使用 pandas 重塑資料

總計欄列(margins)

fifa_players.pivot_table(index=["first", "last"], columns="movement", aggfunc="count", margins=True)
                                attacking                  overall
          movement  passing shooting  All    passing shooting  All
    First     Last                
Cristiano  Ronaldo        2        1    3          2        1    3
   Lionel    Messi        2        1    3          2        1    3
      All                 4        2    6          4        2    6
使用 pandas 重塑資料

用 pivot 還是 pivot table?

 

每個索引/欄位配對是否有多個值?

你是否需要在結果 DataFrame 中建立多重索引?

你是否需要對大型 DataFrame 做摘要統計?

是的!請用 .pivot_table()

使用 pandas 重塑資料

一起來練習吧!

使用 pandas 重塑資料

Preparing Video For Download...