Reputation: 468
How would you compare two (or more columns) values ONLY if another column value is True. Ideally, the output would just be True(if everything is matching correctly) False otherwise.
Something like that: df['value1'].equals(df['value2'])
but only if df['isValid'] is true.
Sorry if this is a stupid question, I am a beginner with panda...
Consider the below Dataframe:
Example1:
isValid value1 value2
True 50 50
True 19 19
False 48 40
Output should be: True as record one and two matches and the "isValid" column is True (meaning we have to compare the values)
Example2:
isValid value1 value2
False 50 50
False 19 19
False 48 40
Output should be True (no comparison to do, nothing wrong then)
Example3:
isValid value1 value2
True 50 50
False 19 19
True 48 40
Output should be False (because record 3 has value1 and value2 different)
Upvotes: 5
Views: 568
Reputation: 75080
here is a way:
df1[df1['isValid']].set_index('isValid').nunique(1).eq(1).all().all()
#True
Upvotes: 3
Reputation: 59549
Here's a simple function that deals with the case of all False is 'isValid' to still return the single bool True
def my_comp(df):
u = df[df.isValid]
if u.empty:
return True
else:
return u['value1'].eq(u['value2']).all()
my_comp(df1)
#True
my_comp(df2)
#True
my_comp(df3)
#False
Upvotes: 1