首页 > 解决方案 > 值错误:一个系列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

问题描述

def answer_three():
    if(df['Gold.2']>=1):
      return df[df['diff']/df['Gold.2'].max()].index[0]
answer_three()

通过编写这段代码,我得到了这样的错误

ValueError:Series 的真值不明确。使用 a.empty, a.bool()....

其中差异是:

df['diff'] = abs(df['Gold'] - df['Gold.1'])

标签: pythonpandasfiltering

解决方案


问题是您正在尝试将 Serie (df['Gold.2']) 与数字进行比较,基本上 Python 看到的内容与此类似:[0 4 6 7 4 2 0 4] >= 1并返回[False True True True True True False True].

因此,当您尝试检查if [False True True True True True False True]python 不知道该怎么做并要求您使用any, empty, all, etc使此列表扁平化为一个的方法之一时。

如果你能解释你想要什么,我可以帮助你修复代码。


推荐阅读