我有以下数据框:
>>> 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,当drive为None或时,该列等于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/