2

I am trying to filter all the columns in a dataframe for the same value:

This is my dataframe:

df=pd.DataFrame({'A':['valid','invalid','valid'],'B':['valid','valid','valid'],'C':['valid','invalid','invalid']})

I want the just the records which has only value 'valid'

What i tried was:

udf=(lambda x: x=='valid')
df1=df.applymap(udf)
df1

       A     B      C
0   True  True   True
1  False  True  False
2   True  True  False

again i dont know have to filter the records that has True alone. how do i do it?

Actual df output
         A      B        C
0    valid  valid    valid
1  invalid  valid  invalid
2    valid  valid  invalid

Expected ouput
       A     B      C
0   valid  valid    valid

can someone help me?

1 Answer 1

3

Compare all values by DataFrame.eq and get rows with all values Trues by DataFrame.all:

df1 = df[df.eq('valid').all(axis=1)]
#same like
#df1 = df[(df == 'valid').all(axis=1)]
print (df1) 
       A      B      C
0  valid  valid  valid

Details:

print (df.eq('valid')) 
       A     B      C
0   True  True   True
1  False  True  False
2   True  True  False

print (df.eq('valid').all(axis=1))
0     True
1    False
2    False
dtype: bool
Sign up to request clarification or add additional context in comments.

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.