简体   繁体   English

根据条件从数据框中删除行

[英]Remove rows from dataframe based on condition

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

如果您的数据帧为df则为df df[~df[df>3].any(axis=1)]

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]),:] 

Something like this should work. 这样的事情应该起作用。

cols = ["a" , "b" , "c"]
greater_than_3 = (df[cols]>3).any(axis=1)
df = df[!greater_than_3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM