Categories
Python Answers

How to create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas?

Spread the love

To create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas, 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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *