【问题标题】:Value Error : The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()值错误:一个系列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
【发布时间】:2020-01-20 03:40:50
【问题描述】:
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'])

【问题讨论】:

标签: python pandas filtering


【解决方案1】:

问题是您试图将 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 方法之一,使该列表扁平化为一个。

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

【讨论】:

  • 实际上有两列,我必须找出哪个国家的夏季和冬季金牌数量差异最大?从 github 个人资料中,我得到了这样的答案 def answer_three(): sub_df = df[(df['Gold'] > 0) & (df['Gold.1'] > 0)].copy() sub_df[' rel'] = sub_df['diff'] / sub_df['Gold.2'] return sub_df[sub_df['rel'] == max(sub_df['rel'])].index[0] answer_three()跨度>
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 2018-03-28
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多