We can also operate on multiple columns at once. If we use
apply without specifying a single column and add the argument
axis=1, the input to our lambda function will be an entire row, not a column. To access particular values of the row, we use the syntax
Suppose we have a table representing a grocery list:
If we want to add in the price with tax for each line, we’ll need to look at two columns:
Is taxed? is
Yes, then we’ll want to multiply
Price by 1.075 (for 7.5% sales tax).
Is taxed? is
No, we’ll just have
Price without multiplying it.
We can create this column using a lambda function and the keyword
df['Price with Tax'] = df.apply(lambda row: row['Price'] * 1.075 if row['Is taxed?'] == 'Yes' else row['Price'], axis=1 )
If an employee worked for more than 40 hours, she needs to be paid overtime (1.5 times the normal hourly wage).
For instance, if an employee worked for 43 hours and made $10/hour, she would receive $400 for the first 40 hours that she worked, and an additional $45 for the 3 hours of overtime, for a total for $445.
Create a lambda function
total_earned that accepts an input
row with keys
hourly_wage and uses an if statement to calculate the hourly wage.
Use the lambda function
apply to add a column
df with the total amount earned by each employee.