【问题标题】:Python: Errormessage: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()Python: Errormessage: ValueError: Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
【发布时间】:2022-01-07 08:54:38
【问题描述】:

我正在使用参数调用以下函数。该参数是某些特定数据帧的键,存储在名为:iNms

的字典中

我的函数目前如下所示: function

但我收到以下错误消息: fehler

当我打印左侧时,我得到“0”,而从右侧我得到一个“假” 我认为因此它不起作用。 我已经尝试转换右侧的条件,但错误仍然存​​在。

你有什么提示吗? 我完全是python新手(1-2周)只有一些理论知识,所以我尝试自己找出我的问题以获得学习经验。

现在我的代码和错误消息为 txt
第一次尝试时,我尝试了这个:

def check_missing_values(name):
    if name in iNms:
        if  iNms[name].iloc[:,[0]].isnull().sum() == 0 and len(name)<8:
            print('hurra')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\t\tKeine fehlenden Primärschlüssel\tOK'.format(name))
        elif iNms[name].iloc[:,[0]].isnull().sum() == 0 and 18>=len(name)>=8:
            print('hurra2')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\tKeine fehlenden Primärschlüssel\tOK'.format(name))   
        elif iNms[name].iloc[:,[0]].isnull().sum() != 0 and len(name)<8:
            print('hurra3')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\t\tEinige oder mehrere Primärschlüssel fehlen. Nicht OK. Nacharbeiten!'.format(name))   
        elif iNms[name].iloc[:,[0]].isnull().sum() !=0 and 18>=len(name)>=8:
            print('hurra4')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\tEinige oder mehrere Primärschlüssel fehlen. Nicht OK. Nacharbeiten!'.format(name))   
    else:
        with open('Log.txt','a') as w:
            if len(name)<17:
                w.write('\n{}\t\tnicht angeliefert'.format(name))
            else:
                w.write('\n{}\tnicht angeliefert'.format(name))
                
                
print('Left side:',iNms['transaction'].iloc[:,[2]].isnull().sum())
print('Left side:',type(iNms['transaction'].iloc[:,[2]].isnull().sum()))
print('Right side:',len('transaction')<8)
print('Right side:',type(len('transaction')<8))
                

我的第二次尝试

def check_missing_values(name):
    if name in iNms:
        if  (iNms[name].iloc[:,[0]].isnull().sum() == 0) & (len(name)<8):
            print('hurra')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\t\tKeine fehlenden Primärschlüssel\tOK'.format(name))
        elif (iNms[name].iloc[:,[0]].isnull().sum() == 0) & (18>=len(name)>=8):
            print('hurra2')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\tKeine fehlenden Primärschlüssel\tOK'.format(name))   
        elif (iNms[name].iloc[:,[0]].isnull().sum() != 0) & (len(name)<8):
            print('hurra3')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\t\tEinige oder mehrere Primärschlüssel fehlen. Nicht OK. Nacharbeiten!'.format(name))   
        elif (iNms[name].iloc[:,[0]].isnull().sum() !=0) & (18>=len(name)>=8):
            print('hurra4')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\tEinige oder mehrere Primärschlüssel fehlen. Nicht OK. Nacharbeiten!'.format(name))   
    else:
        with open('Log.txt','a') as w:
            if len(name)<17:
                w.write('\n{}\t\tnicht angeliefert'.format(name))
            else:
                w.write('\n{}\tnicht angeliefert'.format(name))

print('Left side:',iNms['transaction'].iloc[:,[2]].isnull().sum())
print('Left side:',type(iNms['transaction'].iloc[:,[2]].isnull().sum()))
print('Right side:',len('transaction')<8)
print('Right side:',type(len('transaction')<8))

我收到以下错误消息:

Left side: transaction_ts    0
dtype: int64
Left side: <class 'pandas.core.series.Series'>
Right side: False
Right side: <class 'bool'>
Traceback (most recent call last):
  File "/home/tayfunuenver/Documents/Python/Parser_2.py", line 217, in <module>
    check_missing_values('customer')
  File "/home/tayfunuenver/Documents/Python/Parser_2.py", line 174, in check_missing_values
    if  iNms[name].iloc[:,[0]].isnull().sum() == 0 and len(name)<8:
  File "/home/tayfunuenver/.local/lib/python3.6/site-packages/pandas/core/generic.py", line 1330, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

【问题讨论】:

  • 请将您的函数和完整的错误消息粘贴为文本。粘贴图像会使人们更难复制粘贴和重现您的问题。
  • 好吧,你是对的。当我回到家时,我会这样做。
  • 我现在更新了我的问题。

标签: python-3.x pandas conditional-statements isnull


【解决方案1】:

@莫兹 我现在已经尝试了您的解决方案。第一个对我有用,第二个不行,但我会再试一次,也许我打错了。感谢您的帮助。

我找到了第三种解决方案,我想与这个社区分享。 同时,我稍微缩短了我的代码。 现在看起来像这样:

def check_missing_values(name):
    
    if name in iNms:
        erstespalte_insight = iNms[name].iloc[:,[0]]
        summiere_nullwerte = erstespalte_insight.isnull().sum().sum()
        if  summiere_nullwerte == 0 and len(name)<8:
            print('hurra')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\t\tKeine fehlenden Primärschlüssel\tOK'.format(name))
        elif summiere_nullwerte == 0 and 18>=len(name)>=8:
            print('hurra2')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\tKeine fehlenden Primärschlüssel\tOK'.format(name))   
        elif summiere_nullwerte != 0 and len(name)<8:
            print('hurra3')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\t\tEinige oder mehrere Primärschlüssel fehlen. Nicht OK. Nacharbeiten!'.format(name))   
        elif summiere_nullwerte !=0 and 18>=len(name)>=8:
            print('hurra4')
            with open('Log.txt','a') as w:
                    w.write('\n{}\t\tEinige oder mehrere Primärschlüssel fehlen. Nicht OK. Nacharbeiten!'.format(name))   
    else:
        with open('Log.txt','a') as w:
            if len(name)<17:
                w.write('\n{}\t\tnicht angeliefert'.format(name))
            else:
                w.write('\n{}\tnicht angeliefert'.format(name))


And the output on the terminal:


tayfunuenver@vmOperations1:~/Documents/Python$ /usr/bin/python3 /home/tayfunuenver/Documents/Python/Parser_2.py
1.Prüfung: ZIP-File erfüllt die Anforderungen des Datums:       2021-09-30      OK


hurra2
hurra
hurra4
hurra

【讨论】:

  • 太棒了——如果我理解正确的话,你现在正在将Series“折叠”成一个可以与and一起使用的整数值
  • 是的,这是正确的。
【解决方案2】:

正如错误消息指出的那样——你的错误在这一行——

if iNms[name].iloc[:,[0]].isnull().sum() == 0 and len(name)&lt;8:

如果你仔细观察,你在这里使用了两种不同类型的对象 -

iNms[name].iloc[:,[0]].isnull().sum() == 0是第一个对象,它返回一个pd.Series,而第二个对象len(name) &lt; 8是一个布尔值。

所以 Python 和 Pandas 会很有帮助地告诉你评估类似的东西

pd.Series([True, True, False]) and True 不明确。

对于您的情况 - 有几个解决方案 -

1

要么使用系列中的bool(),因为您的系列只有 1 个值 -

(df.iloc[:, [0]].isnull().sum() == 0).bool() and (len(name) &lt; 8)

2

或使用&amp; 运算符-

(df.iloc[:, [0]].isnull().sum() == 0) &amp; (len(name) &lt; 8)

【讨论】:

  • 我现在已经尝试了您的解决方案。第一个对我有用,第二个不行,但我会再试一次,也许我打错了。
【解决方案3】:

你的错误在于比较

iNms[name].iloc[:,[0]].isnull().sum() == 0

您显然是在将一个系列(sum()-函数的返回值)与一个整数进行比较。为确保sum() 的结果始终可以与整数0 进行比较,您可以编写

int(iNms[name].iloc[:,[0]].isnull().sum()) == 0

【讨论】:

  • 我也会试试这个。谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 2020-03-05
  • 2020-09-28
  • 2019-12-11
  • 2021-11-20
相关资源
最近更新 更多