Kvothe
Kvothe

Reputation: 1393

Pandas boolean operation

I have a dataframe as follows:

ref   check        result
1     age          true
1     gender       false
1     address      false
1     nationality  true

I'm trying to create a new column of 1's and 0's if the following condition is satisfied.

if age == 'true' & (gender == 'false' or address == 'false') & nationality == 'true', then 1 else 0.

This is the code that I have

df['test']= ((df['check']=='age' & df['result']=='true') & ((df['check']=='gender' / df['check']=='address') & df['result']=='false') & (df['check']=='nationality' & df['result']=='true')).astype('int')

But it doesn't work.

Upvotes: 0

Views: 391

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78556

What you're looking for in place of the / is |. But I would generally not advise to use bitwise operators for conditionals. They'll work when the operands evaluate to True and False but would give wrong results when the conditionals are say ints or you need to short circuit.

On another note the precedence of & and | is higher than that of == so that expression won't do what you actually expect e.g:

df['check']=='age' & df['result']=='true'

is evaluated as:

df['check']== ('age' & df['result']) =='true' 

You can wrap the operators in parenthesis to change the order of evaluation:

(df['check']== 'age') & (df['result']) =='true')

Update @kennytm

You don't need and or or in this case just use the parens appropriately.

Upvotes: 2

Related Questions