Using Lambda with a function that takes an argument from different columns of a dataframe
I want to learn how to use lambdas with this type of setting, without using a for loop, which the function takes arguments from rows from two columns of a data block and writes 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)
+3
source to share
3 answers
You can call apply
in df passing arg axis=1
, this will iterate over the row, you can then select the columns of interest in lambda
to jump to your function:
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
Please note that use should be avoided apply
, most operations can be done with vectorized methods, I know this is for learning only, but you should look for numpy or another ufunc that is vectorized
+2
source to share