desmond
desmond

Reputation: 2081

how to apply lambda functions to rows meeting a condition in PANDAS

I am trying to apply a function row-wise to a pandas dataframe with the following

df.loc[df.var1==123,'coltoChange']=df.apply(lambda row: myfunc(row.var1, row.var2),axis=1)

The idea is to apply the function only to rows where value of var1 is 123. But when I run this statement, it still tries to run every single row in the dataframe.

Upvotes: 4

Views: 8403

Answers (1)

jezrael
jezrael

Reputation: 863341

I believe you need filter in both sides:

mask = df.var1==123
df.loc[mask, 'coltoChange']=df[mask].apply(lambda row: myfunc(row.var1, row.var2),axis=1)

Sample:

df = pd.DataFrame({'var1':[123,123,3],
                   'var2':[3,4,5],
                   'var3':[1,2,3],
                   'coltoChange':[2,4,5]})

def myfunc(x,y):
    #sample function
    return x + y

mask = df.var1==123
df.loc[mask, 'coltoChange']=df[mask].apply(lambda row: myfunc(row.var1, row.var2),axis=1)
print (df)
   coltoChange  var1  var2  var3
0          126   123     3     1
1          127   123     4     2
2            5     3     5     3

Upvotes: 6

Related Questions