Pandas pivot_table percentile / quantile

Can percentiles or quantile be used as aggfunc in pandas pivot table? I've tried both numpy.percentile and pandas quantized with no success.

+3


source to share


1 answer


Fictitious data:

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

seems to work fine?

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

      

+4


source







All Articles