How to apply a function to two columns of Pandas dataframe

前端 未结 12 1372
名媛妹妹
名媛妹妹 2020-11-22 06:17

Suppose I have a df which has columns of \'ID\', \'col_1\', \'col_2\'. And I define a function :

f = lambda x, y : my_function_expres

12条回答
  •  生来不讨喜
    2020-11-22 06:38

    If you have a huge data-set, then you can use an easy but faster(execution time) way of doing this using swifter:

    import pandas as pd
    import swifter
    
    def fnc(m,x,c):
        return m*x+c
    
    df = pd.DataFrame({"m": [1,2,3,4,5,6], "c": [1,1,1,1,1,1], "x":[5,3,6,2,6,1]})
    df["y"] = df.swifter.apply(lambda x: fnc(x.m, x.x, x.c), axis=1)
    

提交回复
热议问题