-1

I am facing a problem when comparing two pandas Serieses. I am passing some series to a function that is supposed to calculate stuff based on a condition, as follows:

di = {'A': [1, 2, 7, 2], 'B': [3, 5, 6, 4], 'C': [9, 8, 7, 4]}
df = pd.DataFrame(di)
def function(s1, s2, s3):
    if s1 > s2:
        s4 = s2 + s3
    else:
        s4 = s1 + s3
    return s4
df['D'] = function(df['A'], df['B'], df['C'])

but I am getting a ValueError: The truth value of a Series is ambiguous. I guess this is because I should be comparing the Series' elements row wise, two by two, and the operator > doesn't work this way.

How could I get the function to work without resorting to for-looping on the Serieses' elements?

  • Please provide a [mcve], see [this post](https://stackoverflow.com/a/20159305/3620003) for pandas-specific guidance. – timgeb Jun 23 '22 at 08:58

1 Answers1

2

Because working with arrays use numpy.where with add s3 - output is Series:

def function(s1, s2, s3):
    return s3 + np.where(s1 > s2, s2, s1)

For ouput 1d array is possible use:

def function(s1, s2, s3):
    return np.where(s1 > s2, s2 + s3, s1 + s3)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252