通过Pandas Group计算任意百分位数 [英] Calculate Arbitrary Percentile on Pandas GroupBy
本文介绍了通过Pandas Group计算任意百分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前在熊猫的GroupBy
对象上有一种median
方法.
Currently there is a median
method on the Pandas's GroupBy
objects.
是否有一种计算任意percentile
的方法(请参阅: http://docs.scipy.org/doc/numpy-dev/reference/genic/numpy.percentile.html )上的分组?
Is there is a way to calculate an arbitrary percentile
(see: http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.percentile.html) on the groupings?
中位数是用q=50
计算百分位数.
Median would be the calcuation of percentile with q=50
.
推荐答案
您要使用quantile
方法:
In [47]: df
Out[47]:
A B C
0 0.719391 0.091693 one
1 0.951499 0.837160 one
2 0.975212 0.224855 one
3 0.807620 0.031284 one
4 0.633190 0.342889 one
5 0.075102 0.899291 one
6 0.502843 0.773424 one
7 0.032285 0.242476 one
8 0.794938 0.607745 one
9 0.620387 0.574222 one
10 0.446639 0.549749 two
11 0.664324 0.134041 two
12 0.622217 0.505057 two
13 0.670338 0.990870 two
14 0.281431 0.016245 two
15 0.675756 0.185967 two
16 0.145147 0.045686 two
17 0.404413 0.191482 two
18 0.949130 0.943509 two
19 0.164642 0.157013 two
In [48]: df.groupby('C').quantile(.95)
Out[48]:
A B
C
one 0.964541 0.871332
two 0.826112 0.969558
这篇关于通过Pandas Group计算任意百分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文