I am trying to take 2 columns in Pandas that contain Boolean values and create a third column that is the OR of these Boolean values.
For example, my dataframe currently contains A and B, and I want to create C.
A B C
True True True
False False False
True False True
False True True
My code:
df['C']=df['A'] or df['B']
I have tried to change the structure of the statement several ways but end up with the same error message:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
解决方案In [436]:
df['C'] = np.logical_or(df['A'], df['B'])
df
Out[436]:
A B C
0 True True True
1 False False False
2 True False True
3 False True True
You can't use or operator here as it becomes ambiguous comparing arrays.
or use the bitwise | operator for array comparisons:
In [445]:
df['C'] = df['A'] | df['B']
df
Out[445]:
A B C
0 True True True
1 False False False
2 True False True
3 False True True