消除给定百分位数上的所有数据 [英] Eliminating all data over a given percentile

查看:59
本文介绍了消除给定百分位数上的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 data 的熊猫 DataFrame ,其列名为 ms .我想消除 data.ms 高于95%百分位数的所有行.现在,我正在这样做:

I have a pandas DataFrame called data with a column called ms. I want to eliminate all the rows where data.ms is above the 95% percentile. For now, I'm doing this:

limit = data.ms.describe(90)['95%']
valid_data = data[data['ms'] < limit]

这行得通,但我想将其推广到任何百分位.最好的方法是什么?

which works, but I want to generalize that to any percentile. What's the best way to do that?

推荐答案

使用 Series.quantile() 方法:

In [48]: cols = list('abc')

In [49]: df = DataFrame(randn(10, len(cols)), columns=cols)

In [50]: df.a.quantile(0.95)
Out[50]: 1.5776961953820687

要过滤掉 df.a 大于或等于第95个百分位数的 df 行,请执行以下操作:

To filter out rows of df where df.a is greater than or equal to the 95th percentile do:

In [72]: df[df.a < df.a.quantile(.95)]
Out[72]:
       a      b      c
0 -1.044 -0.247 -1.149
2  0.395  0.591  0.764
3 -0.564 -2.059  0.232
4 -0.707 -0.736 -1.345
5  0.978 -0.099  0.521
6 -0.974  0.272 -0.649
7  1.228  0.619 -0.849
8 -0.170  0.458 -0.515
9  1.465  1.019  0.966

这篇关于消除给定百分位数上的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆