Piyush S. Wanare
Piyush S. Wanare

Reputation: 4943

Pandas how to check values of specific columns is same

I have 5 columns in data-frame called 'A','B','C','D','E'. I want to filter data-frame where values of columns 'A','C'and 'E' are equal.

I have done the following :

OutputDF = DF[DF['A']==DF['C']==DF['E']]

Its giving error as follows:

 ValueError: Truth value of series is ambiguous. Use a.empty, a.boolean,a.item(),a.any() or a.all()

Upvotes: 0

Views: 44

Answers (2)

Paritosh Singh
Paritosh Singh

Reputation: 6246

To address why this happens:

import pandas as pd
DF = pd.DataFrame({"A": [1, 2],
                   "C": [1, 2],
                   "E": [1, 2],
                   })
OutputDF = DF[DF['A']==DF['C']==DF['E']]
#ValueError: The truth value of a Series is ambiguous.

The issue is that, due to how operator chaining works, DF['A']==DF['C']==DF['E'] is being interpreted as

DF['A']==DF['C'] and DF['C']==DF['E']

Essentially, we are attempting to do a boolean and between two Series, and thus we see our error. Since a Series should be giving multiple values, while and expects a single value on both sides of the operator, Thus there is ambiguity on how to reduce the Series on either sides to a single value.

If you wanted to write the condition correctly, you could use bitwise and (&) instead as follows (the brackets are important with bitwise &):

OutputDF = DF[(DF['A']==DF['C']) & (DF['C']==DF['E'])]

Upvotes: 3

jezrael
jezrael

Reputation: 863711

You can compare all filtered columns by list by first column of list by DataFrame.eq and test if all values are True by DataFrame.all:

print (df)
   A  B  C  D  E
0  1  2  3  4  5
1  1  2  1  4  1
2  2  2  2  4  2

L = ['A','C','E']
df = df[df[L].eq(df[L[0]], axis=0).all(axis=1)]
print (df)
   A  B  C  D  E
1  1  2  1  4  1
2  2  2  2  4  2

Upvotes: 3

Related Questions