I have a dataframe as follows:
ref check result
1 age true
1 gender false
1 address false
1 nationality true
if age == 'true' & (gender == 'false' or address == 'false') & nationality == 'true', then 1 else 0.
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')
What you're looking for in place of the
|. But I would generally not advise to use bitwise operators for conditionals. They'll work when the operands evaluate to
False but would give wrong results when the conditionals are say ints or you need to short circuit.
On another note the precedence of
| 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')
You don't need
or in this case.