How to apply a function to two columns of Python Pandas dataframe?

To apply a function to two columns of Python Pandas dataframe, we can use the apply method.

For instance, we write

df['col_3'] = df.apply(lambda x: f(x.col_1, x.col_2), axis=1)

to call apply on data frame df with a lambda function that calls function f on the col_1 and col_2 column values.

And then we assign the returned values to column col_3.