Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Lambda with function that takes argument from different columns of dataframe

I want to learn how to use lambdas with this type of setting without using a for loop which a function takes arguments from rows of two columns of the dataframe and write the result to another column.

import pandas as pd

df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})

print(df)

df["C"] = ""

print(df)

def add_num(num1 ,num2):
    return num1 + num2

for i in range(len(df)):
   df["C"][i] = add_num(df["A"][i], df["B"][i])
print(df)
like image 235
s900n Avatar asked Feb 10 '26 15:02

s900n


1 Answers

You can call apply on the df passing arg axis=1 this will iterate row wise, you can then sub-select the columns of interest in the lambda to pass to your func:

In [49]:    
df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
df["C"] = ""
​
def add_num(num1 ,num2):
    return num1 + num2
​
df["C"] = df.apply(lambda x: add_num(x["A"],  x["B"]), axis=1)
print(df)

   A  B  C
0  1  2  3
1  2  3  5
2  3  4  7

Note that one should avoid using apply, most operations can be performed using vectorised methods, I know this is just for learning but you should look for a numpy or other ufunc that is vectorised

like image 98
EdChum Avatar answered Feb 12 '26 04:02

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!