Python:为什么 np.where 不适用于两个条件?

标签 python pandas numpy conditional-operator

我有以下数据框:

>>> 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时,该列等于0 >驱动器两轮驱动。在其他情况下,我希望该列等于 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

请你帮帮我,我做错了什么?为什么我的代码不起作用?

最佳答案

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

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

关于Python:为什么 np.where 不适用于两个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60524372/

相关文章:

python - 对 next() 和 __iter__() 的确切工作原理感到困惑

python - 如何为 Python 中的对象添加 "attach"功能,例如到 Pandas 数据框?

pandas - 重置 Pandas 中分类索引的类别

Python:逐行替换数据帧值

python - Scrapy不抓取下一页url

python - 在多进程模式下运行 Nose 测试时,导入未得到正确处理

python - 通过删除 numpy 数组来释放内存

python - 将图像(np.array)转换为二值图像

python - GEMM 使用 Numpy einsum

python - 遍历两个字典中的共享键