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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With