使用 pivot 重塑資料

使用 pandas 重塑資料

Maria Eugenia Inzaugarat

Data Scientist

由長轉寬

  • 呈現兩欄之間的關係
  • 對變數做時間序列操作
  • 需要欄為唯一變數的操作
1 https://pandas.pydata.org/docs/user_guide/reshaping.html
使用 pandas 重塑資料

由長轉寬

長格式的 DataFrame

使用 pandas 重塑資料

Pivot 方法

從長格式轉為寬格式的箭頭

 

pandas 的 pivot 方法呼叫

使用 pandas 重塑資料

Pivot 方法

從長格式轉為寬格式的箭頭

 

帶引數的 pivot 呼叫

使用 pandas 重塑資料

Pivot 方法

長寬 DataFrame,欄與索引高亮

 

高亮含欄名的 index 引數

使用 pandas 重塑資料

Pivot 方法

長寬 DataFrame,欄與欄名高亮

 

高亮含欄名的 columns 引數

使用 pandas 重塑資料

Pivot 方法

長寬 DataFrame,欄與值高亮

 

高亮含欄名的 values 引數

使用 pandas 重塑資料

Pivot 方法

高亮的 NaN 儲存格值

 

高亮含欄名的各引數

使用 pandas 重塑資料

對資料集做 Pivot

fifa = pd.read_csv('fifa_players.csv')
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
使用 pandas 重塑資料

對資料集做 Pivot

fifa.pivot(index='name'                                            )
使用 pandas 重塑資料

對資料集做 Pivot

fifa.pivot(index='name', columns='variable'                        )
使用 pandas 重塑資料

對資料集做 Pivot

fifa.pivot(index='name', columns='variable', values='metric_system')
         variable  height   weight
             name        
Cristiano Ronaldo     187       83
         J. Oblak     188       87
使用 pandas 重塑資料

同時 Pivot 多個欄位

fifa.pivot(index='name', columns='variable', values=['metric_system', 'imperial_system'])
                     metric_system     imperial_system       
         variable   height  weight     height   weight
             name                                                         
Cristiano Ronaldo      187      83       6.13    183.0
         J. Oblak      188      87       6.16    191.0
使用 pandas 重塑資料

同時 Pivot 多個欄位

 

帶階層欄索引的長轉寬箭頭

 

高亮 index 與 columns 引數及其欄名

使用 pandas 重塑資料

同時 Pivot 多個欄位

fifa.pivot(index="name", columns="variable")
                     metric_system     imperial_system       
         variable   height  weight     height   weight
             name                                                         
Cristiano Ronaldo      187      83       6.13    183.0
         J. Oblak      188      87       6.16    191.0
使用 pandas 重塑資料

重複項錯誤

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
使用 pandas 重塑資料

重複項錯誤

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 <--
使用 pandas 重塑資料

重複項錯誤

another_fifa.pivot(index="name", columns="variable")
ValueError: Index contains duplicate entries, cannot reshape

 

another_fifa = another_fifa.drop(4, axis=0)
another_fifa.pivot(index="name", columns="variable")
                     metric_system     imperial_system       
         variable   height  weight     height   weight
             name                                                         
Cristiano Ronaldo      187      83       6.13    183.0
         J. Oblak      188      87       6.16    191.0
使用 pandas 重塑資料

一起來練習吧!

使用 pandas 重塑資料

Preparing Video For Download...