tool.ish
tool.ish

Reputation: 101

Getting percentiles by row in Python/Pandas

I have a Dataframe for which I want percentiles row-wise. Although, the documentation of pandas.quantile() [0.14] method does include an axis= argument, the result is not as expected:

In [4]: df = pd.DataFrame(np.random.randn(6, 3), columns=['a', 'b', 'c'])

In [5]: df
Out[5]:
          a         b         c
0 -0.815200  0.137148 -1.378515
1  0.106762  0.689793 -0.261648
2 -1.013495  0.807520  1.211062
3 -0.182480  1.082589 -0.825990
4  0.480081  0.864027 -0.192584
5 -2.081455 -0.003881 -1.003602

In [6]: df.quantile(0.95,axis=1)
Out[6]: 
a    0.386751
b    1.027949
c    0.860151
dtype: float64

In [7]: df.quantile(0.95,axis=0)
Out[7]: 
a    0.386751
b    1.027949
c    0.860151
dtype: float64

Just wanted to get it to your attention. Also, wanted to know what would be the most efficient workaround for this.

Upvotes: 0

Views: 1472

Answers (1)

hvedrung
hvedrung

Reputation: 467

Could not reproduce this as well. But df.T.quantile(0.95) should work.

Upvotes: 2

Related Questions