user96564
user96564

Reputation: 1607

Selection of Boolean in pandas between one main column and another optional column

Sorry, I couldn't find any good title for this question. Please feel free to edit the title if you find some suitable title for it.

I have a dataframe and it has two columns Val1 and ExtraVal; both Val1 and ExtraVal contain boolean values. My main column is Val1 but at the same time i am supposed to take values from ExtraVal too if it is only True and that True also matches with True values in the Val1 column.

I cannot use df[val1] & df[ExtraVal] because then that statement will remove the True from Val1 column when ExtraVal becomes False.

and I cannot use or statement either between these two because then it becomes True if ExtraColumn is true even though val1 column is false.

Hope this sample data explains more clearly what I meant.

Sample Dataframe looks like this

Val1 ExtraVal
True,False
False,False
False,False
True,True
False,True
False,True
True,True

Output I want is

Val1,ExtraVal
True,False
True,True
True,True

Any suggestions ?

Upvotes: 1

Views: 41

Answers (1)

jezrael
jezrael

Reputation: 863166

I think need:

df1 = df[df['Val1']]
print (df1)
   Val1  ExtraVal
0  True     False
3  True      True
6  True      True

Upvotes: 2

Related Questions