Python: Why Is Np.where Not Working With Two Conditions?
I have the following data frame: >>> import pandas as pd >>> import numpy as np >>> df_test = pd.DataFrame({'id': [100, 101, 102, 103, 104], 'drive': ['4
Solution 1:
Add parentheses because priority precedence of |
operator (bitwise OR):
df_test['is_4x4'] = np.where(pd.isnull(df_test['drive']) | (df_test['drive'] == '2WD'), 0, 1)
Or use Series.eq
:
df_test['is_4x4'] = np.where(df_test['drive'].isna() | df_test['drive'].eq('2WD'), 0, 1)
You can check docs - 6.16. Operator precedence where see |
have higher priority as ==
:
Operator Description
lambda Lambda expression
if – else Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in, is, is not, Comparisons, including membership tests
<, <=, >, >=, !=, == and identity tests
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
(expressions...), [expressions...], Binding or tuple display, list display,
{key: value...}, {expressions...} dictionary display, set display
Post a Comment for "Python: Why Is Np.where Not Working With Two Conditions?"