我可以从条件函数中返回多个值并单独打印它们吗?

发布于 2025-02-12 18:41:37 字数 819 浏览 2 评论 0原文

我想从函数返回多个值并分别打印它们(不是作为元组)。多重返回是简单的函数(第一部分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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2025-02-19 18:41:37

尝试以下操作:

import pandas as pd


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]
}
df = pd.DataFrame(dataset)

print(df)


# this part of code is ok to return muntiple values
def quanta1(data):
        return data['A']+data['B'], data['A']-data['B']


a, b = quanta1(df)
print(a)
print(b)


# I want to return two values a and b  with the if condition
def quanta2(data):
    data = data[data["C"] > 0]
    return data['A']+data['B'], data['A']-data['B']


a, b = quanta2(df)
print(a)
print(b)

try this:

import pandas as pd


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]
}
df = pd.DataFrame(dataset)

print(df)


# this part of code is ok to return muntiple values
def quanta1(data):
        return data['A']+data['B'], data['A']-data['B']


a, b = quanta1(df)
print(a)
print(b)


# I want to return two values a and b  with the if condition
def quanta2(data):
    data = data[data["C"] > 0]
    return data['A']+data['B'], data['A']-data['B']


a, b = quanta2(df)
print(a)
print(b)
川水往事 2025-02-19 18:41:37

正如评论中指出的那样,您面临的第一个问题是,您不能将系列迫使一个布尔值进行评估以评估if语句。这就是错误消息所指出的:

valueerror:系列的真实价值是模棱两可的。使用A.Empty,A.Bool(),A.Item(),A.Any()或A.all()

如果您要运行操作,如果某些汇总统计量是正确的,则可以使用其中之一减少,例如

def quanta(data):
    if (data['C'] > 0).any():
        return ((data['A']+data['B']),(data['A']-data['B']))

,一旦解决此问题,您仍然会有一个情况,a,b = Quanta()可能会导致问题,因为Quanta将返回none 如果不满足条件。

您可以通过处理功能返回的可能情况来处理此问题。如果Quanta评估false中的条件,将返回。因此,只需在您的代码中处理它:

quanta_result = quanta()
if quanta_result is not None:
    a, b = quanta_result

另一方面,如果您实际上是在尝试执行元素操作,则可能正在寻找 series.where ?您可以执行各种蒙版操作,例如:

def quanta(data):
    # in rows where C > 0, return A + B, and return A - B elsewhere
    return (data["A"] + data["B"]).where(
        data["C"] > 0, 
        (data["A"] - data["B"]),
    )

这只是一个例子 - 我不确定您要做什么。

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:

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

If you're trying to run the operation if some aggregate statistic is true, you can use one of these reductions, e.g.

def quanta(data):
    if (data['C'] > 0).any():
        return ((data['A']+data['B']),(data['A']-data['B']))

once you resolve this, you will still have a case where a, b = quanta() can cause problems, as quanta will return None 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:

quanta_result = quanta()
if quanta_result is not None:
    a, b = quanta_result

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.:

def quanta(data):
    # in rows where C > 0, return A + B, and return A - B elsewhere
    return (data["A"] + data["B"]).where(
        data["C"] > 0, 
        (data["A"] - data["B"]),
    )

This is just an example - I'm not totally sure what you're going for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文