python pandas- apply function with two arguments to columns

后端 未结 3 1915
借酒劲吻你
借酒劲吻你 2020-12-04 14:32

Can you make a python pandas function with values in two different columns as arguments?

I have a function that returns a 1 if two columns have values in the same ra

相关标签:
3条回答
  • 2020-12-04 14:57

    You don't really need a lambda function if you are defining the function outside:

    def segmentMatch(vec):
        RealTime = vec[0]
        ResponseTime = vec[1]
        if RealTime <= 566 and ResponseTime <= 566:
            matchVar = 1
        elif 566 < RealTime <= 1132 and 566 < ResponseTime <= 1132:
            matchVar = 1
        elif 1132 < RealTime <= 1698 and 1132 < ResponseTime <= 1698:
            matchVar = 1
        else:
            matchVar = 0
        return matchVar
    
    df['NewCol'] = df[['TimeCol', 'ResponseCol']].apply(segmentMatch, axis=1)
    

    If "segmentMatch" were to return a vector of 2 values instead, you could do the following:

    def segmentMatch(vec):
        ......
        return pd.Series((matchVar1, matchVar2)) 
    
    df[['NewCol', 'NewCol2']] = df[['TimeCol','ResponseCol']].apply(segmentMatch, axis=1)
    
    0 讨论(0)
  • 2020-12-04 14:58

    Why not just do this?

    df['NewCol'] = df.apply(lambda x: segmentMatch(x['TimeCol'], x['ResponseCol']), axis=1)
    

    Rather than trying to pass the column as an argument as in your example, we now simply pass the appropriate entries in each row as argument, and store the result in 'NewCol'.

    0 讨论(0)
  • 2020-12-04 15:21

    A chain-friendly way to perform this operation is via assign():

    df.assign( NewCol = lambda x: segmentMatch(x['TimeCol'], x['ResponseCol']) )
    
    0 讨论(0)
提交回复
热议问题