Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 消除给定百分位上的所有数据_Python_Pandas_Filtering_Percentile - Fatal编程技术网

Python 消除给定百分位上的所有数据

Python 消除给定百分位上的所有数据,python,pandas,filtering,percentile,Python,Pandas,Filtering,Percentile,我有一个名为data的pandasDataFrame和一个名为ms的列。我想消除data.ms高于95%百分位数的所有行。就目前而言,我正在这样做: limit = data.ms.describe(90)['95%'] valid_data = data[data['ms'] < limit] limit=data.ms.descripe(90)['95%] 有效数据=数据[数据['ms']

我有一个名为
data
的pandas
DataFrame
和一个名为
ms
的列。我想消除
data.ms
高于95%百分位数的所有行。就目前而言,我正在这样做:

limit = data.ms.describe(90)['95%']
valid_data = data[data['ms'] < limit]
limit=data.ms.descripe(90)['95%]
有效数据=数据[数据['ms']<限制]
这是可行的,但我想把它推广到任何百分位。最好的方法是什么?

使用以下方法:

要筛选出
df
df.a
大于或等于第95百分位的行,请执行以下操作:

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
[72]中的
:df[df.a
numpy在这方面比熊猫快得多:

numpy.percentile(df.a,95) # attention : the percentile is given in percent (5 = 5%)
相当于,但比以下速度快3倍:

df.a.quantile(.95)  # as you already noticed here it is ".95" not "95"
因此,对于您的代码,它提供:

df[df.a < np.percentile(df.a,95)]
df[df.a
使用pandas,如果我想比较不同的col和特定的分位数,是否有类似numpy广播的快速方法?当删除所有列时,它是否也起作用,即
df[df
?如果不在范围内,我希望所有值都被过滤掉,并在需要时替换为
NaN
。基本相同但更简洁:
df.query('a
。如果列名很长,可以提高可读性:
col='some_verbose_metric_name';df.query(f'{col}<{col}.quantile(.95)
可以确认,如果您能够提供列提取,numpy的实现会更快cost@2diabolos.com是否有一种方法可以在多个列上实现百分位筛选。类似于df[numpy.logical_和(df.adf[df.a < np.percentile(df.a,95)]