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


This should do it:



import pandas as pd

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

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

df['C'] = df.T.apply(lambda x: add_num(x['A'], x['B']))

      

0


source


The obvious answer would be: don't use lambda

when simpler expressions work:

df['C'] = df['A'] + df['B']

      

Because this will use vectorized operations.

The approach with lambda

and is apply

already included in @ EdChum's answer, so I won't show it anymore.

0


source







All Articles