pandas 根据条件从数据框中删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47931248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove rows from dataframe based on condition
提问by Gavin Gamble
I know this has to have been addressed before, but I cannot seem to find an answer that works
我知道之前必须解决这个问题,但我似乎找不到有效的答案
I have the columns that I want to test the condition against and I want to remove all rows where their value in any of the three columns is above a given value.
我有要测试条件的列,我想删除三列中任何一列中的值高于给定值的所有行。
x a b c d
1 2 1 3 4
2 3 5 2 2
3 3 3 3 2
4 1 2 3 3
if I ran against this dataframe, with my cutoff value being anything greater than 3, then I should be returned with
如果我针对这个数据框运行,并且我的截止值大于 3,那么我应该返回
x a b c d
3 3 3 3 2
4 1 2 3 3
回答by rpanai
If your dataframe is df
then df[~df[df>3].any(axis=1)]
如果您的数据帧是df
那么df[~df[df>3].any(axis=1)]
回答by Sahil Dahiya
You can remove rows like:
您可以删除如下行:
import pandas as pd
import numpy as np
df.loc[df.x>=3,:]
You can also use conditions using numpy logical_and and logical_or if you have upper and lower limit
如果您有上限和下限,您还可以使用 numpy logical_and 和 logical_or 使用条件
df = df.loc[np.logical_and(dd.x<=3,df.x<=0),:]
You can also use ~
还可以用~
df.loc[~df.x.isin([1,2]),:]
回答by Gabriel A
Something like this should work.
像这样的事情应该有效。
cols = ["a" , "b" , "c"]
greater_than_3 = (df[cols]>3).any(axis=1)
df = df[!greater_than_3]