溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用Python實(shí)現(xiàn)數(shù)據(jù)的透視表

發(fā)布時(shí)間:2021-04-09 11:50:29 來源:億速云 閱讀:249 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)怎么用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

在處理數(shù)據(jù)時(shí),經(jīng)常需要對(duì)數(shù)據(jù)分組計(jì)算均值或者計(jì)數(shù),在Microsoft Excel中,可以通過透視表輕易實(shí)現(xiàn)簡(jiǎn)單的分組運(yùn)算。而對(duì)于更加復(fù)雜的分組運(yùn)算,Python中pandas包可以幫助我們實(shí)現(xiàn)。

1 數(shù)據(jù)

首先引入幾個(gè)重要的包:

import pandas as pd
import numpy as np
from pandas import DataFrame,Series

通過代碼構(gòu)造數(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 交叉表—分類計(jì)數(shù)

按照不同類進(jìn)行計(jì)數(shù)統(tǒng)計(jì)是最常見透視功能,可以通

(1)crosstab

#函數(shù):
crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, dropna=True, normalize=False)

crosstab的index和columns是必須要指定復(fù)制的參數(shù):

pd.crosstab(data.key1,data.key2)

結(jié)果如下:

key2 one three two
key1     
a  3  1 1
b  0  1 1
c  1  1 1

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

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

結(jié)果:

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ù)同樣可以實(shí)現(xiàn),運(yùn)算函數(shù)默認(rèn)值aggfunc='mean',指定為aggfunc='count'即可:

data.pivot_table('num1',index='key1',columns='key2',aggfunc='count')

結(jié)果相同:

key2 one three two
key1     
a  3  1 1
b  1  1 1
c  1  1 2

(3)groupby

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

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

結(jié)果:

key2 one three two
key1     
a  3  1 1
b  1  1 1
c  1  1 2

3 其它透視表運(yùn)算

(1)pivot_table

pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All')

要進(jìn)行何種運(yùn)算,只需要指定aggfunc即可。

默認(rèn)計(jì)算均值:

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')

結(jié)果:

   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ù):

#定義一個(gè)最大值減最小值的函數(shù)
def max_min (group):
 return group.max()-group.min()
data.pivot_table(index='key1',columns='key2',aggfunc=max_min)

結(jié)果:

   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可以直接應(yīng)用:

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

返回結(jié)果:

   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

感謝各位的閱讀!關(guān)于“怎么用Python實(shí)現(xiàn)數(shù)據(jù)的透視表”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI