透視表(pivot table)數據匯總分析工具。
根據一個或多個鍵對數據進行聚合,並根據行和列上的分組鍵將數據分配到各個矩形區域中。
1、在Python和pandas中,通過groupby功能以及重塑運算制作透視表
2、DataFrame有一個pivot_table 方法
3、頂級的pandas.pivot_table函數
pivot_table 可以添加分項小計
小費數據集,根據sex和smoker計算分組平均值,並將sex和smoker放到行上
1 #書中是rows,但是沒辦法運行 2 tips.pivot_table(index=['sex','smoker']) 3 Out[42]: 4 size tip tip_pct total_bill 5 sex smoker 6 Female No 2.592593 2.773519 0.156921 18.105185 7 Yes 2.242424 2.931515 0.182150 17.977879 8 Male No 2.711340 3.113402 0.160669 19.791237 9 Yes 2.500000 3.051167 0.152771 22.284500
只聚合tip_pct和size,根據day分組,smoker在列上,day 在行上:
1 tips.pivot_table(['tip_pct','size'],index=['sex','day'],columns='smoker') 2 Out[43]: 3 size tip_pct 4 smoker No Yes No Yes 5 sex day 6 Female Fri 2.500000 2.000000 0.165296 0.209129 7 Sat 2.307692 2.200000 0.147993 0.163817 8 Sun 3.071429 2.500000 0.165710 0.237075 9 Thur 2.480000 2.428571 0.155971 0.163073 10 Male Fri 2.000000 2.125000 0.138005 0.144730 11 Sat 2.656250 2.629630 0.162132 0.139067 12 Sun 2.883721 2.600000 0.158291 0.173964 13 Thur 2.500000 2.300000 0.165706 0.164417
傳入margins=True添加分項小計
1 tips.pivot_table(['tip_pct','size'],index=['sex','day'], 2 columns='smoker',margins=True) 3 Out[44]: 4 size tip_pct 5 smoker No Yes All No Yes All 6 sex day 7 Female Fri 2.500000 2.000000 2.111111 0.165296 0.209129 0.199388 8 Sat 2.307692 2.200000 2.250000 0.147993 0.163817 0.156470 9 Sun 3.071429 2.500000 2.944444 0.165710 0.237075 0.181569 10 Thur 2.480000 2.428571 2.468750 0.155971 0.163073 0.157525 11 Male Fri 2.000000 2.125000 2.100000 0.138005 0.144730 0.143385 12 Sat 2.656250 2.629630 2.644068 0.162132 0.139067 0.151577 13 Sun 2.883721 2.600000 2.810345 0.158291 0.173964 0.162344 14 Thur 2.500000 2.300000 2.433333 0.165706 0.164417 0.165276 15 All 2.668874 2.408602 2.569672 0.159328 0.163196 0.160803
想使用其他聚合函數,將其傳給aggfunc
tips.pivot_table('tip_pct',index=['sex','smoker'],columns='day', aggfunc=len,margins=True) Out[45]: day Fri Sat Sun Thur All sex smoker Female No 2.0 13.0 14.0 25.0 54.0 Yes 7.0 15.0 4.0 7.0 33.0 Male No 2.0 32.0 43.0 20.0 97.0 Yes 8.0 27.0 15.0 10.0 60.0 All 19.0 87.0 76.0 62.0 244.0
存在NA值,設置一個fill_value:
1 tips.pivot_table('size',index=['time','sex','smoker'],columns='day',aggfunc='sum',fill_value=0) 2 Out[46]: 3 day Fri Sat Sun Thur 4 time sex smoker 5 Dinner Female No 2 30 43 2 6 Yes 8 33 10 0 7 Male No 4 85 124 0 8 Yes 12 71 39 0 9 Lunch Female No 3 0 0 60 10 Yes 6 0 0 17 11 Male No 0 0 0 50 12 Yes 5 0 0 23
pivot_table參數:
values:待聚合的列的名稱,默認聚合所有的數值列。
index:用於分組的列名或其他分組鍵,出現在結果透視表的行
columns:用於分組的列名或其他分組鍵,出現在結果透視表的列
aggfunc:聚合函數或函數列表,默認為'mean'。
fill_value:用於替換結果表中的缺失值
margins:添加行/列小計和總計,默認為False
交叉表
交叉表(crosstab)是一種用於計算分組頻率的特殊透視表。
1 #書中沒有這個data數據,自己創建一個 2 data=pd.read_excel(r'C:\Users\Administrator\Desktop\data.xlsx') 3 4 data 5 Out[50]: 6 Sample Gender Handedness 7 0 1 Female Right-handed 8 1 2 Male Left-handed 9 2 3 Female Right-handed 10 3 4 Male Right-handed 11 4 5 Male Left-handed 12 5 6 Male Right-handed 13 6 7 Female Right-handed 14 7 8 Female Left-handed 15 8 9 Male Right-handed 16 9 10 Female Right-handed 17 #使用pandas.crosstab 18 pd.crosstab(data.Gender,data.Handedness,margins=True) 19 Out[51]: 20 Handedness Left-handed Right-handed All 21 Gender 22 Female 1 4 5 23 Male 2 3 5 24 All 3 7 10 25 26 #crosstab的前2個參數可以是數組、Series或數組列表 27 pd.crosstab([tips.time,tips.day],tips.smoker,margins=True) 28 Out[52]: 29 smoker No Yes All 30 time day 31 Dinner Fri 3 9 12 32 Sat 45 42 87 33 Sun 57 19 76 34 Thur 1 0 1 35 Lunch Fri 1 6 7 36 Thur 44 17 61 37 All 151 93 244
crosstab函數的參數: