Challenge - 5 Problems
Apply Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of apply() with lambda on DataFrame column
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3, 4]}) result = df['A'].apply(lambda x: x**2 if x % 2 == 0 else x) print(result.tolist())
Attempts:
2 left
💡 Hint
Remember the lambda squares only even numbers.
✗ Incorrect
The lambda function squares the number only if it is even (2 and 4). Odd numbers remain unchanged.
❓ data_output
intermediate2:00remaining
Resulting DataFrame after apply() with lambda on multiple columns
Given the DataFrame and the apply() operation below, what is the resulting DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]}) result = df.apply(lambda row: row['X'] + row['Y'], axis=1) print(result)
Attempts:
2 left
💡 Hint
axis=1 means apply function to each row.
✗ Incorrect
The lambda adds values in columns 'X' and 'Y' for each row: 1+3=4 and 2+4=6.
🔧 Debug
advanced2:00remaining
Identify the error in apply() with lambda on DataFrame
What error does the following code raise?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) result = df.apply(lambda x: x + 1) print(result)
Attempts:
2 left
💡 Hint
Check what apply does by default on DataFrame without axis specified.
✗ Incorrect
By default, apply applies the function to each column (Series). Adding 1 to a Series adds 1 to each element, so no error occurs.
🚀 Application
advanced2:00remaining
Using apply() with lambda to categorize data
You have a DataFrame with a column 'score'. You want to create a new column 'grade' where scores >= 90 get 'A', scores >= 80 get 'B', else 'C'. Which code correctly does this?
Pandas
import pandas as pd df = pd.DataFrame({'score': [95, 82, 74, 88]})
Attempts:
2 left
💡 Hint
Check the comparison operators and method used.
✗ Incorrect
Option A uses apply on the 'score' column with correct conditions and inclusive >= operators.
🧠 Conceptual
expert2:00remaining
Understanding apply() behavior with lambda on DataFrame axis
Consider a DataFrame df with shape (3, 2). What is the shape of the output when running df.apply(lambda x: x.sum(), axis=1)?
Attempts:
2 left
💡 Hint
axis=1 applies function to each row, returning one value per row.
✗ Incorrect
Applying sum row-wise returns a Series with one value per row, so shape is (3,).