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: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
我正在调用以下带参数的函数。该参数是某些特定数据帧的键,存储在名为:iNms
的字典中
我的函数目前看起来像这样:
function
但我收到以下错误消息:
fehler
当我打印左侧时,我得到“0”,而从右侧打印时,我得到“False”
因此我认为这是行不通的。
我已经尝试转换右侧的条件,但错误仍然存在。
你有什么提示要给我吗?
我是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().
正如错误消息指出的那样 - 你的错误在这一行 -
if iNms[name].iloc[:,[0]].isnull().sum() == 0 and len(name)<8:
如果你仔细观察,你在这里使用了两种不同类型的对象 -
iNms[name].iloc[:,[0]].isnull().sum() == 0
是第一个对象,它returns是一个pd.Series
,而第二个对象len(name) < 8
是一个布尔值。
所以 Python 和 Pandas 有助于告诉您评估
pd.Series([True, True, False]) and True
有歧义。
对于你的情况 - 有几个解决方案 -
1
要么使用系列中的 bool()
,因为您的系列只有 1 个值 -
(df.iloc[:, [0]].isnull().sum() == 0).bool() and (len(name) < 8)
2
或使用&
运算符-
(df.iloc[:, [0]].isnull().sum() == 0) & (len(name) < 8)
你的错误是为了比较
iNms[name].iloc[:,[0]].isnull().sum() == 0
您显然是在将一个系列(sum()
-函数的 return-值)与一个整数进行比较。为确保 sum()
的结果始终可以与整数 0
进行比较,您可以编写
int(iNms[name].iloc[:,[0]].isnull().sum()) == 0
@Mortz
我现在已经尝试了您的解决方案。第一个对我有用,第二个对我不起作用,但我会再试一次,也许我打错了字。感谢您的帮助。
我找到了第三个解决方案,我想与这个社区分享。
与此同时,我稍微缩短了我的代码。
现在看起来像这样:
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
我正在调用以下带参数的函数。该参数是某些特定数据帧的键,存储在名为:iNms
的字典中我的函数目前看起来像这样: function
但我收到以下错误消息: fehler
当我打印左侧时,我得到“0”,而从右侧打印时,我得到“False” 因此我认为这是行不通的。 我已经尝试转换右侧的条件,但错误仍然存在。
你有什么提示要给我吗? 我是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().
正如错误消息指出的那样 - 你的错误在这一行 -
if iNms[name].iloc[:,[0]].isnull().sum() == 0 and len(name)<8:
如果你仔细观察,你在这里使用了两种不同类型的对象 -
iNms[name].iloc[:,[0]].isnull().sum() == 0
是第一个对象,它returns是一个pd.Series
,而第二个对象len(name) < 8
是一个布尔值。
所以 Python 和 Pandas 有助于告诉您评估
pd.Series([True, True, False]) and True
有歧义。
对于你的情况 - 有几个解决方案 -
1
要么使用系列中的 bool()
,因为您的系列只有 1 个值 -
(df.iloc[:, [0]].isnull().sum() == 0).bool() and (len(name) < 8)
2
或使用&
运算符-
(df.iloc[:, [0]].isnull().sum() == 0) & (len(name) < 8)
你的错误是为了比较
iNms[name].iloc[:,[0]].isnull().sum() == 0
您显然是在将一个系列(sum()
-函数的 return-值)与一个整数进行比较。为确保 sum()
的结果始终可以与整数 0
进行比较,您可以编写
int(iNms[name].iloc[:,[0]].isnull().sum()) == 0
@Mortz 我现在已经尝试了您的解决方案。第一个对我有用,第二个对我不起作用,但我会再试一次,也许我打错了字。感谢您的帮助。
我找到了第三个解决方案,我想与这个社区分享。 与此同时,我稍微缩短了我的代码。 现在看起来像这样:
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