Python:为什么 np.where 不能在两个条件下工作?

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

我有以下数据框:

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

我想创建一个新列 is_4x4,当 driveNone,或者 驱动器2WD。在其他情况下,我希望该列等于 1。

我正在使用以下代码,但结果与我预期的不一样:

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

我想要的输出如下:

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

拜托,你能帮帮我吗,我做错了什么?为什么我的代码不起作用?

添加括号是因为 | 运算符的优先级(按位或):

df_test['is_4x4'] = np.where(pd.isnull(df_test['drive']) | (df_test['drive'] == '2WD'), 0, 1)

或使用Series.eq:

df_test['is_4x4'] = np.where(df_test['drive'].isna() | df_test['drive'].eq('2WD'), 0, 1)

您可以查看 docs - 6.16。运算符优先级 | 具有更高的优先级,如 ==:

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