用python做透视表_用Python实现数据的透视表的方法
用Python實現數據的透視表的方法
來源:中文源碼網????瀏覽: 次????日期:2019年11月5日
【下載文檔:??用Python實現數據的透視表的方法.txt?】
(友情提示:右鍵點上行txt文檔名->目標另存為)
用Python實現數據的透視表的方法在處理數據時,經常需要對數據分組計算均值或者計數,在Microsoft Excel中,可以通過透視表輕易實現簡單的分組運算。而對于更加復雜的分組運算,Python中pandas包可以幫助我們實現。
1 數據
首先引入幾個重要的包:
import pandas as pd
import numpy as np
from pandas import DataFrame,Series通過代碼構造數據集:
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)})得到數據集如下:
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.1574262 交叉表—分類計數
按照不同類進行計數統計是最常見透視功能,可以通
(1)crosstab
#函數:
crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, dropna=True, normalize=False)crosstab的index和columns是必須要指定復制的參數:
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
函數:
pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All')使用pivot_table函數同樣可以實現,運算函數默認值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 23 其它透視表運算
(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也可以使用其它自定義函數:
#定義一個最大值減最小值的函數
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
普通的函數如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實現數據的透視表的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持中文源碼網。
親,試試微信掃碼分享本頁!?*^_^*
總結
以上是生活随笔為你收集整理的用python做透视表_用Python实现数据的透视表的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米13限量定制色明天限量发售!12+5
- 下一篇: java和python混合编程_浅谈C+