gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:28:35 25 4
gpt4 key购买 nike

我有以下数据框:

>>> 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,当驱动器为无或驱动器为 2WD 时,该列将等于 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

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

最佳答案

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com