Python错误提示:TypeError: ufunc 'bitwise_or' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
错误代码:
df['new_col'] = df.apply(lambda x: 0 if df['a'] == 0 | df['b'] == 0)
搜索之后,问题在于两个if条件都需要用括号括起来,修改如下:
df['new_col'] = df.apply(lambda x: 0 if (df['a'] == 0) | (df['b'] == 0))
继续报错:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
继续修改,搜索到这个回答# Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
上联有各种解释,最后根据情况需求选择了一种方案,修改如下:
df['new_col'] = df.apply(lambda x: 0 if (df['a'] == 0).any() | (df['b'] == 0).any())