【问题标题】:'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()。
【发布时间】:2018-07-27 08:42:09
【问题描述】:

我在 Python 上使用简单的函数:

def liste_bis(data):

Country_Name = []
Platform_Category = []
Platform = []
Country_Acronym = []

for m,n in enumerate (np.arange(np.shape(data)[0])):

    if (data["domain1"][m]=='afe'):

        Country_Name.append('France')
        Platform_Category.append('App')
        Platform.append('BDDF-HB App')
        Country_Acronym.append('FR')


    elif (data["domain1"][m]=='afer'):

        Country_Name.append('France')
        Platform_Category.append('Site')
        Platform.append('BDDF-HB Site')
        Country_Acronym.append('FR')


    elif (data["domain1"][m]=='afert'):

        Country_Name.append('France')
        Platform_Category.append('App')
        Platform.append('BDDF-BNP App')
        Country_Acronym.append('FR')


    elif (data["domain1"][m]=='aferty'):

        Country_Name.append('France')
        Platform_Category.append('Site')
        Platform.append('BDDF-BNP Site')
        Country_Acronym.append('FR')

    elif (data["domain1"][m]=='afertyu'):

        Country_Name.append('Luxembourg')
        Platform_Category.append('App')
        Platform.append('BGL-BNP App')
        Country_Acronym.append('LU')

dictionnaire = {"Country_Name":Country_Name,"Platform_Category":Platform_Category,"Platform":Platform,"Country_Acronym":Country_Acronym}

return(dictionnaire)

但是我有一些麻烦。 当我执行程序时,它会返回我:

'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'

但是当我将这个函数与只有 1 行的 DataFrame 一起使用时,它可以正常工作。 但是当我有超过 1 行时,它不起作用......

让您展示我使用的数据框示例: dataframe_example

你能帮帮我吗?

谢谢

【问题讨论】:

  • data["domain1"][m] 有多个元素时,您希望data["domain1"][m]=='afe' 表现如何。是否应该只在data["domain1"][m]中的所有元素都满足条件时才输入if?
  • 你的代码缩进不好,这在​​ Python 中很关键。此外,它并不完整,因为您没有显示该函数的调用方式、参数值的定义,甚至 numpy 的导入。请阅读并关注How to create a Minimal, Complete, and Verifiable example
  • @FlyingTeller data["domain1"][m] 中只有 1 个元素
  • 您能否添加数据示例和所需输出?
  • @zipa 好的,我完成了

标签: python pandas numpy


【解决方案1】:

为什么它只适用于单元素结果:当你有一个多元素系列时,它的“真值”可能是一个真值系列,或者它可能是“都是 all 这些值中的 True" 等。只有一行,就没有这种歧义。所以选择错误消息推荐的显式方法之一(取决于你真正追求的是什么),然后继续。

【讨论】:

  • 感谢您的回复。但我只有一个元素,所以我不明白错误信息
  • 你的数组data有多少维?听起来好像有三个。如果m 是一个整数,则data["domain1"][m] 的维度更少,并且您正在比较所有维度,即其中的每个元素 与字符串。
  • @alexis 我刚刚修改了我的帖子,您可以在图像中看到我的 DataFrame 的结构,谢谢
  • 这并没有告诉我,np.shape(data) 的输出会更有用(而且看在上帝的份上,请不要作为图像!)。无论如何,我会从你的描述中走出来。您没有说哪个代码行导致了错误,也没有给出完整的错误跟踪。如果我误解了,请解决您的问题。我们想提供帮助,但您需要帮助我们帮助您。
  • 感谢您的帮助@alexis 我的英语不好,这就是为什么我的问题不好。
猜你喜欢
  • 2017-09-23
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2021-02-01
  • 2019-05-04
  • 2021-11-27
相关资源
最近更新 更多