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

╄→尐↘猪︶ㄣ 提交于 2021-02-16 21:19:08

问题


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?


回答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


来源:https://stackoverflow.com/questions/60524372/python-why-is-np-where-not-working-with-two-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!