Reputation: 2981
If I want to use the following syntax in Pandas to create an if
condition using comparison operators, do I need to wrap the or
conditions within their own parenthesis?
Without extra parenthesis
df.loc[(df['Col1'] == 'Yes') &
(df['Col2'] == 'Yes') &
(df['Col3'] == 'No') |
(df['Col3'] == 'Yes') |
(df['Col3'] == 'Maybe'),
['Result']] = 'CORRECT'
With extra parenthesis
df.loc[(df['Col1'] == 'Yes') &
(df['Col2'] == 'Yes') &
((df['Col3'] == 'No') |
(df['Col3'] == 'Yes') |
(df['Col3'] == 'Maybe')),
['Result']] = 'CORRECT'
Or are both valid? It's hard to deduce from the documentation which should be correct.
Upvotes: 1
Views: 208
Reputation: 862761
I think it is better to use isin
:
df.loc[(df['Col1'] == 'Yes') &
(df['Col2'] == 'Yes') &
(df['Col3'].isin(['No', 'Yes', 'Maybe'])), 'Result'] = 'CORRECT'
But your statements are different:
@DeepSpace - comment:
It has nothing to do directly with pandas, but with operator precedence in Python. The bitwise and (&) has precedence over bitwise or (|) so in this case the parenthesis are required.
np.random.seed(1245)
a = ['No', 'Yes', 'Maybe']
df = pd.DataFrame(np.random.choice(a, size=(10, 3)), columns=['Col1','Col2','Col3'])
df.loc[(df['Col1'] == 'Yes') &
(df['Col2'] == 'Yes') &
(df['Col3'] == 'No') |
(df['Col3'] == 'Yes') |
(df['Col3'] == 'Maybe'),
'Result1'] = 'CORRECT'
df.loc[(df['Col1'] == 'Yes') &
(df['Col2'] == 'Yes') &
((df['Col3'] == 'No') |
(df['Col3'] == 'Yes') |
(df['Col3'] == 'Maybe')),
'Result2'] = 'CORRECT'
df.loc[(df['Col1'] == 'Yes') &
(df['Col2'] == 'Yes') &
(df['Col3'].isin(['No', 'Yes', 'Maybe'])), 'Result3'] = 'CORRECT'
print (df)
Col1 Col2 Col3 Result1 Result2 Result3
0 Maybe Yes Yes CORRECT NaN NaN
1 No Maybe No NaN NaN NaN
2 Yes No Maybe CORRECT NaN NaN
3 Maybe Yes No NaN NaN NaN
4 Yes Yes Maybe CORRECT CORRECT CORRECT
5 Yes Maybe Yes CORRECT NaN NaN
6 Maybe Maybe Yes CORRECT NaN NaN
7 Maybe No Yes CORRECT NaN NaN
8 Maybe Maybe Maybe CORRECT NaN NaN
9 Yes Yes No CORRECT CORRECT CORRECT
Upvotes: 2