Challenge - 5 Problems
Apply Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of apply() with a lambda on DataFrame columns
What is the output of this code when applying a lambda function to each column of the DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) result = df.apply(lambda x: x.max() - x.min()) print(result)
Attempts:
2 left
💡 Hint
Think about what max() and min() do for each column.
✗ Incorrect
The lambda function calculates the difference between the maximum and minimum values in each column. For column A, max is 3 and min is 1, difference is 2. For column B, max is 6 and min is 4, difference is 2.
❓ data_output
intermediate2:00remaining
Result of apply() with a custom function on columns
Given this DataFrame, what is the result of applying the custom function to each column?
Pandas
import pandas as pd def custom_func(col): return col.sum() / len(col) df = pd.DataFrame({ 'X': [10, 20, 30], 'Y': [5, 15, 25] }) result = df.apply(custom_func) print(result)
Attempts:
2 left
💡 Hint
The function calculates the average of each column.
✗ Incorrect
The function sums the values in each column and divides by the number of rows, which is the mean. For X: (10+20+30)/3 = 20. For Y: (5+15+25)/3 = 15.
🔧 Debug
advanced2:00remaining
Identify the error in apply() usage on columns
What error does this code raise when applying the function to each column?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) result = df.apply(lambda x: x["nonexistent"]) print(result)
Attempts:
2 left
💡 Hint
Check if the column has the key you are trying to access.
✗ Incorrect
The lambda tries to access a key 'nonexistent' in each column (which is a Series). Since this key does not exist, a KeyError is raised.
❓ visualization
advanced2:00remaining
Visualize the effect of apply() on columns with a plotting function
Which option correctly applies a function to plot the sum of each column as a bar chart?
Pandas
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) # Which code snippet below correctly plots the sum of each column as a bar chart?
Attempts:
2 left
💡 Hint
Sum the columns first, then plot the result.
✗ Incorrect
Option C sums each column using apply(sum), resulting in a Series with sums, then plots it as a bar chart. Other options either apply plot incorrectly or apply sum in wrong order.
🚀 Application
expert3:00remaining
Use apply() on columns to normalize data
You want to normalize each column in a DataFrame by subtracting the mean and dividing by the standard deviation. Which code snippet correctly does this using apply() on columns?
Pandas
import pandas as pd df = pd.DataFrame({ 'X': [10, 20, 30], 'Y': [5, 15, 25] }) # Choose the correct code to normalize columns
Attempts:
2 left
💡 Hint
Normalization uses mean and standard deviation of each column separately.
✗ Incorrect
Option B correctly normalizes each column by subtracting its own mean and dividing by its own standard deviation. Option B uses overall DataFrame mean/std which is incorrect. Option B uses median instead of mean. Option B does min-max scaling, not normalization.