pandas.pivot_table
pandas.pivot_table(data,values = None,index = None郭怪,columns = None,aggfunc ='mean'刊橘,fill_value = None鄙才,margins = False,dropna = True促绵,margins_name ='All' )
創(chuàng)建一個(gè)電子表格樣式的數(shù)據(jù)透視表作為DataFrame攒庵。數(shù)據(jù)透視表中的級(jí)別將存儲(chǔ)在結(jié)果DataFrame的索引和列上的MultiIndex對(duì)象(層次索引)中。
參數(shù):
數(shù)據(jù) : DataFrame
values : 要聚合的列败晴,可選
index : 列浓冒,Grouper,數(shù)組或前一個(gè)列表
如果傳遞數(shù)組尖坤,則它必須與數(shù)據(jù)的長(zhǎng)度相同稳懒。該列表可以包含任何其他類型(列表除外)。在數(shù)據(jù)透視表索引上分組的鍵慢味。如果傳遞數(shù)組场梆,則它的使用方式與列值相同。
columns : 列纯路,Grouper辙谜,數(shù)組或前一個(gè)列表
如果傳遞數(shù)組,則它必須與數(shù)據(jù)的長(zhǎng)度相同感昼。該列表可以包含任何其他類型(列表除外)。在數(shù)據(jù)透視表列上分組的鍵罐脊。如果傳遞數(shù)組定嗓,則它的使用方式與列值相同。
aggfunc : function萍桌,function of list宵溅,dict,default numpy.mean
如果傳遞的函數(shù)列表上炎,生成的數(shù)據(jù)透視表將具有分層列恃逻,其頂層是函數(shù)名稱(從函數(shù)對(duì)象本身推斷)如果傳遞dict雏搂,則鍵是要聚合的列,值是函數(shù)或函數(shù)列表
fill_value : 標(biāo)量寇损,默認(rèn)無(wú)
用于替換缺失值的值
margin : boolean凸郑,默認(rèn)為False
添加所有行/列(例如,對(duì)于小計(jì)/總計(jì))
dropna : 布爾值矛市,默認(rèn)為True
不包括條目全部為NaN的列
margins_name : string芙沥,默認(rèn)為'All'
當(dāng)margin為True時(shí),將包含總計(jì)的行/列的名稱浊吏。
返回:
table : DataFrame
也可以看看
DataFrame.pivot
沒(méi)有聚合的Pivot可以處理非數(shù)字?jǐn)?shù)據(jù)而昨。
Examples
df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
... "bar", "bar", "bar", "bar"],
... "B": ["one", "one", "one", "two", "two",
... "one", "one", "two", "two"],
... "C": ["small", "large", "large", "small",
... "small", "large", "small", "small",
... "large"],
... "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
... "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
df
A B C D E
0 foo one small 1 2
1 foo one large 2 4
2 foo one large 2 5
3 foo two small 3 5
4 foo two small 3 6
5 bar one large 4 6
6 bar one small 5 8
7 bar two small 6 9
8 bar two large 7 9
This first example aggregates values by taking the sum.
table = pivot_table(df, values='D', index=['A', 'B'],
... columns=['C'], aggfunc=np.sum)
table
C large small
A B
bar one 4 5
two 7 6
foo one 4 1
two NaN 6
We can also fill missing values using the fill_value parameter.
table = pivot_table(df, values='D', index=['A', 'B'],
... columns=['C'], aggfunc=np.sum, fill_value=0)
table
C large small
A B
bar one 4 5
two 7 6
foo one 4 1
two 0 6
The next example aggregates by taking the mean across multiple columns.
table = pivot_table(df, values=['D', 'E'], index=['A', 'C'],
... aggfunc={'D': np.mean,
... 'E': np.mean})
table
D E
mean mean
A C
bar large 5.500000 7.500000
small 5.500000 8.500000
foo large 2.000000 4.500000
small 2.333333 4.333333
We can also calculate multiple types of aggregations for any given value column.
table = pivot_table(df, values=['D', 'E'], index=['A', 'C'],
... aggfunc={'D': np.mean,
... 'E': [min, max, np.mean]})
table
D E
mean max mean min
A C
bar large 5.500000 9 7.500000 6
small 5.500000 9 8.500000 8
foo large 2.000000 5 4.500000 4
small 2.333333 6 4.333333 2