Home > Net >  Can I return multiple values from conditional function and print them separately?
Can I return multiple values from conditional function and print them separately?

Time:07-05

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().

CodePudding user response:

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)

CodePudding user response:

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.

  • Related