Home > Software design >  How to compare row-wise two `pd.Series`es without for loops
How to compare row-wise two `pd.Series`es without for loops

Time:06-23

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?

CodePudding user response:

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)
  • Related