【发布时间】:2020-06-21 07:08:47
【问题描述】:
cf1p2['Age Check'] = (cf1p2['Age']>18 & cf1p2['Age']<60)
我想检查 cf1p2['Age'] 是否介于 18 和 60 之间,并且需要 cf1p2['Age Check'] 列中的结果
当前代码给出以下错误。
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
【问题讨论】:
-
请发布具有预期结果的数据。以此为指导:stackoverflow.com/questions/20109391/…
-
cf1p2['Age Check'] = cf1p2['Age'].between(18, 60, inclusive=False) -
尝试
cf1p2.loc[(cf1p2['Age']>18) & (cf1p2['Age']<60), 'new_col'] = value您也可以使用np.where基本上将数据框转换为真正的假布尔值,您需要将其作为数据框的表达式访问或返回,或使用pandas/numpy 方法将系列/数据框转换为布尔值 -
这能回答你的问题吗? pandas equivalent of np.where