简体   繁体   English

如何在没有for循环的情况下逐行比较两个`pd.Series`es

[英]How to compare row-wise two `pd.Series`es without for loops

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 .但我得到一个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?如果不对 Serieses 的元素使用 for 循环,我如何才能使该功能正常工作?

Because working with arrays use numpy.where with add s3 - output is Series :因为使用数组使用numpy.where和 add s3 - 输出是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM