WilliamW
WilliamW

Reputation: 468

Compare two or more columns values only if another column value is True

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

Answers (3)

BENY
BENY

Reputation: 323226

I will do

df.eval('value1==value2')[df.isValid].all()

Upvotes: 5

anky
anky

Reputation: 75080

here is a way:

df1[df1['isValid']].set_index('isValid').nunique(1).eq(1).all().all()
#True

Upvotes: 3

ALollz
ALollz

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

Related Questions