是否可以使用百分位数或分位数作为 pandas 数据透视表中的 aggfunc?我试过 numpy.percentile 和 pandas quantile 都没有成功。
最佳答案
虚拟数据:
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
关于python - pandas pivot_table 百分位数/分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302220/