0

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.

Kvothe
  • 1,341
  • 7
  • 20
  • 33

1 Answers1

2

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.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • Oh thanks! I didn't know about that. I'll stick to and and or. – Kvothe Nov 02 '16 at 16:47
  • I'm pretty sure you *can't* use `and` here, since `&`, `|`, `~` have special meanings in Pandas (numpy). See http://stackoverflow.com/questions/21415661/logic-operator-for-boolean-indexing-in-pandas – kennytm Nov 02 '16 at 16:57
  • @kennytm I actually forgot that numpy feature. Thanks for the pointer – Moses Koledoye Nov 02 '16 at 17:01