Hey there,

I’m working in ‘applying labda to a row’, and I don’t understand why the following code should return a Syntax Error:

total_earned = lambda row: 
row['hours_worked'] * 10
if row['hours_worked'] <= 40 
else 400 + (row['hours_worked'] - 40)* 15

df['total_earned'] = df.apply(total_earned, axis = 1)


If it’s not a copy paste issue in that code formatting it seems you have tried to split a lambda expression onto multiple lines without any sort of line continuation. Whilst you could use explicit \ line joining to provide continuation considering the complexity of your function why not write an actual function. Since it’s neither short not anonymous there’s no benefit to using lambda.


That should be it! Thanks again tgrtim!

1 Like