pandas pivot_table百分位数/分位数

时间:2014-12-04 19:20:26

标签: python numpy pandas

是否可以使用百分位数或分位数作为pandas数据透视表中的aggfunc?我已经尝试了numpy.percentile和pandas分位数都没有成功。

1 个答案:

答案 0 :(得分:6)

虚拟数据:

In [135]: df = pd.DataFrame([['a',2,3],
                             ['a',5,6],
                             ['a',7,8], 
                             ['b',9,10], 
                             ['b',11,12], 
                             ['b',13,14]], columns=list('abc'))

np.percentile似乎工作正常吗?

In [140]: df.pivot_table(columns='a', aggfunc=lambda x: np.percentile(x, 50))
Out[140]: 
a  a   b
b  5  11
c  6  12