Python: Why is np.where not working with two conditions?

Jaroslav Bezděk :

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': ['4WD', None, '4WD', None, '2WD']})
>>> print(df_test)
    id drive
0  100   4WD
1  101  None
2  102   4WD
3  103  None
4  104   2WD

And I would like to make a new column is_4x4, that would be equal to 0, when drive is None, or drive is 2WD. In other cases, I would like the column to be equal to 1.

I am using the following code, but the result is not as I expected:

>>> df_test['is_4x4'] = np.where(pd.isnull(df_test['drive']) | df_test['drive'] == '2WD', 0, 1)
>>> print(df_test)
    id drive  is_4x4
0  100   4WD       1
1  101  None       1
2  102   4WD       1
3  103  None       1
4  104   2WD       1

My desired output is following:

    id drive  is_4x4
0  100   4WD       1
1  101  None       0
2  102   4WD       1
3  103  None       0
4  104   2WD       0

Please, could you help me, what I am doing wrong? Why is my code not working?

jezrael :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23718&siteId=1