How to apply a function to two columns of Pandas dataframe

前端 未结 12 1306
名媛妹妹
名媛妹妹 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:48

    The method you are looking for is Series.combine. However, it seems some care has to be taken around datatypes. In your example, you would (as I did when testing the answer) naively call

    df['col_3'] = df.col_1.combine(df.col_2, func=get_sublist)
    

    However, this throws the error:

    ValueError: setting an array element with a sequence.
    

    My best guess is that it seems to expect the result to be of the same type as the series calling the method (df.col_1 here). However, the following works:

    df['col_3'] = df.col_1.astype(object).combine(df.col_2, func=get_sublist)
    
    df
    
       ID   col_1   col_2   col_3
    0   1   0   1   [a, b]
    1   2   2   4   [c, d, e]
    2   3   3   5   [d, e, f]
    

提交回复
热议问题