【发布时间】:2019-06-28 13:34:32
【问题描述】:
我写了以下代码:
df.loc[((df['col_A'] == False) & (df['loc_B'] == False)), 'col_C'] = "abc"
但是 "abc" 被写入 col_C 的所有值而不是条件中指定的值。这是为什么呢?
编辑:
我的列是对象/字符串,所以我尝试使用以下函数对其进行转换:
def str_to_bool(s):
if s == 'True':
return True
elif s == 'False':
return False
但它会引发以下错误:
"The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()".
【问题讨论】:
-
你对
df.dtypes有什么看法?如果你有布尔系列,你应该更喜欢~df['col_A']到df['col_A'] == False。如果不这样做,则需要将系列转换为布尔值。 -
它们是对象,请看我的编辑
标签: python pandas bit-manipulation pandas-loc