0

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().
resunga
  • 43
  • 3
  • 1
    The problem is that `data['C'] > 0` doesn't have a single truth value, not that you are trying to return multiple values from the function. – chepner Jul 04 '22 at 23:09
  • ahh nice catch. yep - see https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o – Michael Delgado Jul 04 '22 at 23:10
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 04 '22 at 23:49

2 Answers2

0

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.

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
  • This completely ignores the actual problem, which is that `data['C'] > 0` does't have a Boolean value. – chepner Jul 05 '22 at 00:20
  • well... the OP actually does ask about this problem. they have another bug in their code, which you point out in comments. but the question is about this topic. – Michael Delgado Jul 05 '22 at 02:01
0

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)
maya
  • 1,029
  • 1
  • 2
  • 7
  • please explain how your code works and how it addresses the question. you've changed the way the OP's code functions significantly, so the answer could really use some text clarification. – Michael Delgado Jul 05 '22 at 02:11
  • Based on your code to modify, quanta1 basically no change, just remove the extra brackets, quanta2 use to pandas boolean index, such as df[df.column > 0], df for a dataframe, column for one of the column names, he will be for each line of the dataframe column column value of each row of the dataframe for filtering, the return value of this is the dataframe composed of all rows with column greater than 0 – maya Jul 05 '22 at 02:57