国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用Python實現數據的透視表的方法

2020-01-04 14:03:31
字體:
來源:轉載
供稿:網友

在處理數據時,經常需要對數據分組計算均值或者計數,在Microsoft Excel中,可以通過透視表輕易實現簡單的分組運算。而對于更加復雜的分組運算,Python中pandas包可以幫助我們實現。

1 數據

首先引入幾個重要的包:

import pandas as pdimport numpy as npfrom 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  num20 a one 0.268705 0.0840911 b two 0.876707 0.2177942 c three 0.229999 0.5744023 a two 0.707990 -1.4444154 c one 0.786064 0.3432445 a one 0.587273 1.2123916 b three 0.927396 1.5053727 a one 0.295271 -0.4976338 c two 0.292721 0.0988149 a three 0.369788 -1.157426

2 交叉表—分類計數

按照不同類進行計數統計是最常見透視功能,可以通

(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 twokey1     a  3  1 1b  0  1 1c  1  1 1

想要在邊框處增加匯總項可以指定margin的值為True:

pd.crosstab(data.key1,data.key2,margins=True)

結果:

key2 one three two Allkey1      a  3  1 1 5b  1  1 1 3c  1  1 2 4All  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 twokey1     a  3  1 1b  1  1 1c  1  1 2

(3)groupby

通過groupby相對來說會更加復雜,首先需要對data按照key1和key2進行聚類,然后進行count運算,再將key2的index重塑為columns:

data.groupby(['key1','key2'])['num1'].count().unstack()

結果:

key2 one three twokey1     a  3  1 1b  1  1 1c  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  twokey1               a  0.193332 0.705657 0.203155 -0.165749 2.398164 -1.293595b  0.167947 0.204545 0.661460 0.555850 -0.522528 0.143530c  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  twokey1               a  0.579996 0.705657 0.203155 -0.497246 2.398164 -1.293595b  0.167947 0.204545 0.661460 0.555850 -0.522528 0.143530c  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  twokey1             a  0.179266 0.0 0.000 3.109405 0.0 0.000000b  0.000000 0.0 0.000 0.000000 0.0 0.000000c  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  twokey1               a  0.193332 0.705657 0.203155 -0.165749 2.398164 -1.293595b  0.167947 0.204545 0.661460 0.555850 -0.522528 0.143530c  0.496993 0.033673 0.206028 -0.115093 0.024650 0.077726

以上這篇用Python實現數據的透視表的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武定县| 聂拉木县| 和林格尔县| 玉溪市| 耒阳市| 盱眙县| 胶州市| 岐山县| 安阳县| 颍上县| 类乌齐县| 达孜县| 界首市| 通河县| 资中县| 沧州市| 德安县| 司法| 杨浦区| 云霄县| 盱眙县| 达拉特旗| 蓝田县| 祁阳县| 吉安县| 台东县| 琼中| 德庆县| 建昌县| 陆良县| 松原市| 苍梧县| 托克托县| 鄯善县| 南平市| 祁门县| 南丰县| 木兰县| 富平县| 武陟县| 文化|