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

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


I will do

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


here is a way:

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


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)#Truemy_comp(df2)#Truemy_comp(df3)#False