我可以从条件函数中返回多个值并单独打印它们吗?
我想从函数返回多个值并分别打印它们(不是作为元组)。多重返回是简单的函数(第一部分OD代码还可以),但是我无法在条件函数中返回两个值。我可以返回多个值并单独打印它们(第二部分)吗?
dataset = {'A': [6, 2, 3, 4, 5, 5, 7, 8, 9, 7],
'B': [5, 1, 2, 3, 4, 4, 6, 7, 8, 6],
'C': [0, 1, 0, 1, 1, 0, 1, 0, 1, 0]}
data = pd.DataFrame(dataset)
#this part of code is ok to return muntiple values
def quanta():
return ((data['A']+data['B']),(data['A']-data['B']))
a, b = quanta()
print(a)
print(b)
#I want to return two values a and b with the if condition. I just add the if statement to the above code it doesn't work.
def quanta():
if data['C'] > 0:
return ((data['A']+data['B']),(data['A']-data['B']))
a, b = quanta()
print(a)
print(b)
#ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), #a.any() or a.all().
I want to return multiple values from the function and print them separately (not as tuple).Multiple return is simple function (first part od code is ok), but I am not able to return two values in conditional function. can I return multiple values and print them separately (for the second part)?
dataset = {'A': [6, 2, 3, 4, 5, 5, 7, 8, 9, 7],
'B': [5, 1, 2, 3, 4, 4, 6, 7, 8, 6],
'C': [0, 1, 0, 1, 1, 0, 1, 0, 1, 0]}
data = pd.DataFrame(dataset)
#this part of code is ok to return muntiple values
def quanta():
return ((data['A']+data['B']),(data['A']-data['B']))
a, b = quanta()
print(a)
print(b)
#I want to return two values a and b with the if condition. I just add the if statement to the above code it doesn't work.
def quanta():
if data['C'] > 0:
return ((data['A']+data['B']),(data['A']-data['B']))
a, b = quanta()
print(a)
print(b)
#ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), #a.any() or a.all().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下操作:
try this:
正如评论中指出的那样,您面临的第一个问题是,您不能将系列迫使一个布尔值进行评估以评估if语句。这就是错误消息所指出的:
如果您要运行操作,如果某些汇总统计量是正确的,则可以使用其中之一减少,例如
,一旦解决此问题,您仍然会有一个情况,
a,b = Quanta()
可能会导致问题,因为Quanta
将返回none 如果不满足条件。
您可以通过处理功能返回的可能情况来处理此问题。如果
Quanta
评估false中的条件,无
将返回。因此,只需在您的代码中处理它:另一方面,如果您实际上是在尝试执行元素操作,则可能正在寻找
series.where
?您可以执行各种蒙版操作,例如:这只是一个例子 - 我不确定您要做什么。
As is pointed out in comments, the first issue you're facing is that you can't coerce a Series to a single boolean needed to evaluate for an if statement. That is what the error message is indicating:
If you're trying to run the operation if some aggregate statistic is true, you can use one of these reductions, e.g.
once you resolve this, you will still have a case where
a, b = quanta()
can cause problems, asquanta
will returnNone
if the condition is not satisfied.you can handle this by handling the possible cases returned by your function. if the condition in
quanta
evaluates False,None
will be returned. so just handle that in your code:If, on the other hand, you're actually trying to perform an elementwise operation, you may be looking for something like
Series.where
? you could do all kinds of masked operations, e.g.:This is just an example - I'm not totally sure what you're going for.