I've dataframe df
from excel
Is this possible in any way:
df["A"] = [foo(b, c) for (b, c) in (df["B"], df["C"])]
need to pass variables in function from different columns of dataframe
thx
I've dataframe df
from excel
Is this possible in any way:
df["A"] = [foo(b, c) for (b, c) in (df["B"], df["C"])]
need to pass variables in function from different columns of dataframe
thx
You can use df.apply()
on axis=1
(for column index) to get the corresponding values of df["B"]
and df["C"])
for each row for passing to foo
, as folllow:
df['A'] = df.apply(lambda x: foo(x['B'], x['C']), axis=1)
This is the more idiomatic and Pandas way of achieving the task. We commonly prefer to use Pandas functions than using list comprehension since Pandas functions can handle NaN
values better while list comprehension often gives you error when handling NaN
values.