1

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

x  a  b  c  d
3  3  3  3  2
4  1  2  3  3
1
  • 1
    "Any of the 3"? You have 5 columns. What columns are you testing on? Commented Dec 21, 2017 at 19:08

3 Answers 3

5

If your dataframe is df then df[~df[df>3].any(axis=1)]

Sign up to request clarification or add additional context in comments.

2 Comments

df[~(df.gt(3)).any(1)] is faster.
@ScottBoston nice. I didn't know about it.
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

df = df.loc[np.logical_and(dd.x<=3,df.x<=0),:] 

You can also use ~

df.loc[~df.x.isin([1,2]),:] 

Comments

0

Something like this should work.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.