我想通过 numpypercentile()
通过pandas的功能agg()
正如我下面对各种其他 numpy 统计函数所做的那样。
现在我有一个如下所示的数据框:
AGGREGATE MY_COLUMN
A 10
A 12
B 5
B 9
A 84
B 22
我的代码如下所示:
grouped = dataframe.groupby('AGGREGATE')
column = grouped['MY_COLUMN']
column.agg([np.sum, np.mean, np.std, np.median, np.var, np.min, np.max])
上面的代码有效,但我想做类似的事情
column.agg([np.sum, np.mean, np.percentile(50), np.percentile(95)])
即,指定要返回的各个百分位数agg()
.
这应该怎么做呢?
也许不是超级高效,但一种方法是自己创建一个函数:
def percentile(n):
def percentile_(x):
return x.quantile(n)
percentile_.__name__ = 'percentile_{:02.0f}'.format(n*100)
return percentile_
然后将其包含在您的agg
:
In [11]: column.agg([np.sum, np.mean, np.std, np.median,
np.var, np.min, np.max, percentile(50), percentile(95)])
Out[11]:
sum mean std median var amin amax percentile_50 percentile_95
AGGREGATE
A 106 35.333333 42.158431 12 1777.333333 10 84 12 76.8
B 36 12.000000 8.888194 9 79.000000 5 22 12 76.8
请注意,确定是这样的should尽管完成...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)