用Python實現(xiàn)數(shù)據(jù)的透視表的方法
在處理數(shù)據(jù)時,經(jīng)常需要對數(shù)據(jù)分組計算均值或者計數(shù),在Microsoft Excel中,可以通過透視表輕易實現(xiàn)簡單的分組運算。而對于更加復雜的分組運算,Python中pandas包可以幫助我們實現(xiàn)。
1 數(shù)據(jù)
首先引入幾個重要的包:
import pandas as pd import numpy as np from pandas import DataFrame,Series
通過代碼構造數(shù)據(jù)集:
data=DataFrame({'key1':['a','b','c','a','c','a','b','a','c','a','b','c'],'key2':['one','two','three','two','one','one','three','one','two','three','one','two'],'num1':np.random.rand(12),'num2':np.random.randn(12)})
得到數(shù)據(jù)集如下:
data key1 key2 num1 num2 0 a one 0.268705 0.084091 1 b two 0.876707 0.217794 2 c three 0.229999 0.574402 3 a two 0.707990 -1.444415 4 c one 0.786064 0.343244 5 a one 0.587273 1.212391 6 b three 0.927396 1.505372 7 a one 0.295271 -0.497633 8 c two 0.292721 0.098814 9 a three 0.369788 -1.157426
2 交叉表—分類計數(shù)
按照不同類進行計數(shù)統(tǒng)計是最常見透視功能,可以通
(1)crosstab
#函數(shù): crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, dropna=True, normalize=False)
crosstab的index和columns是必須要指定復制的參數(shù):
pd.crosstab(data.key1,data.key2)
結果如下:
key2 one three two key1 a 3 1 1 b 0 1 1 c 1 1 1
想要在邊框處增加匯總項可以指定margin的值為True:
pd.crosstab(data.key1,data.key2,margins=True)
結果:
key2 one three two All key1 a 3 1 1 5 b 1 1 1 3 c 1 1 2 4 All 5 3 4 12
(2)pivot_table
函數(shù):
pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All')
使用pivot_table函數(shù)同樣可以實現(xiàn),運算函數(shù)默認值aggfunc='mean',指定為aggfunc='count'即可:
data.pivot_table('num1',index='key1',columns='key2',aggfunc='count')
結果相同:
key2 one three two key1 a 3 1 1 b 1 1 1 c 1 1 2
(3)groupby
通過groupby相對來說會更加復雜,首先需要對data按照key1和key2進行聚類,然后進行count運算,再將key2的index重塑為columns:
data.groupby(['key1','key2'])['num1'].count().unstack()
結果:
key2 one three two key1 a 3 1 1 b 1 1 1 c 1 1 2
3 其它透視表運算
(1)pivot_table
pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All')
要進行何種運算,只需要指定aggfunc即可。
默認計算均值:
data.pivot_table(index='key1',columns='key2')
out:
num1 num2 key2 one three two one three two key1 a 0.193332 0.705657 0.203155 -0.165749 2.398164 -1.293595 b 0.167947 0.204545 0.661460 0.555850 -0.522528 0.143530 c 0.496993 0.033673 0.206028 -0.115093 0.024650 0.077726
分類匯總呢并求和:
data.pivot_table(index='key1',columns='key2',aggfunc='sum')
結果:
num1 num2 key2 one three two one three two key1 a 0.579996 0.705657 0.203155 -0.497246 2.398164 -1.293595 b 0.167947 0.204545 0.661460 0.555850 -0.522528 0.143530 c 0.496993 0.033673 0.412055 -0.115093 0.024650 0.155452
也可以使用其它自定義函數(shù):
#定義一個最大值減最小值的函數(shù) def max_min (group): return group.max()-group.min()
data.pivot_table(index='key1',columns='key2',aggfunc=max_min)
結果:
num1 num2 key2 one three two one three two key1 a 0.179266 0.0 0.000 3.109405 0.0 0.000000 b 0.000000 0.0 0.000 0.000000 0.0 0.000000 c 0.000000 0.0 0.177 0.000000 0.0 1.609466
(2)通過groupby
普通的函數(shù)如mean,sum可以直接應用:
data.groupby(['key1','key2']).mean().unstack()
返回結果:
num1 num2 key2 one three two one three two key1 a 0.193332 0.705657 0.203155 -0.165749 2.398164 -1.293595 b 0.167947 0.204545 0.661460 0.555850 -0.522528 0.143530 c 0.496993 0.033673 0.206028 -0.115093 0.024650 0.077726
以上這篇用Python實現(xiàn)數(shù)據(jù)的透視表的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決Python設置函數(shù)調(diào)用超時,進程卡住的問題
今天小編就為大家分享一篇解決Python設置函數(shù)調(diào)用超時,進程卡住的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python趣味挑戰(zhàn)之實現(xiàn)簡易版音樂播放器
小伙伴們天天學編程應該都學累了,今天特地給大家整理了這篇文章,讓大家在學習的時候也收貨快樂,文中有非常詳細的代碼示例,需要的朋友可以參考下2021-05-05Django ORM 聚合查詢和分組查詢實現(xiàn)詳解
這篇文章主要介紹了Django ORM 聚合查詢和分組查詢實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08