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
.