Challenge - 5 Problems
GroupBy Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of GroupBy with a custom aggregation function
What is the output of this code snippet that groups data by 'Category' and applies a custom function to the 'Value' column?
Pandas
import pandas as pd data = pd.DataFrame({ 'Category': ['A', 'A', 'B', 'B', 'C'], 'Value': [10, 20, 10, 30, 50] }) def custom_func(x): return x.max() - x.min() result = data.groupby('Category')['Value'].agg(custom_func) print(result)
Attempts:
2 left
💡 Hint
Think about what the custom function does: max minus min for each group.
✗ Incorrect
The custom function calculates the difference between the maximum and minimum values in each group. For group A, max is 20 and min is 10, difference 10. For B, max 30, min 10, difference 20. For C, only one value 50, difference 0.
❓ data_output
intermediate1:30remaining
Number of groups after grouping with a custom function
Given this DataFrame, how many groups are formed when grouping by the 'Type' column?
Pandas
import pandas as pd data = pd.DataFrame({ 'Type': ['X', 'Y', 'X', 'Z', 'Y', 'Z', 'Z'], 'Score': [5, 10, 15, 10, 20, 30, 40] }) groups = data.groupby('Type') count = len(groups)
Attempts:
2 left
💡 Hint
Count unique values in the 'Type' column.
✗ Incorrect
The 'Type' column has three unique values: 'X', 'Y', and 'Z'. Grouping by this column creates 3 groups.
🔧 Debug
advanced2:00remaining
Identify the error in custom aggregation function usage
What error does this code raise when trying to apply a custom function to a groupby object?
Pandas
import pandas as pd data = pd.DataFrame({ 'Group': ['A', 'A', 'B'], 'Value': [1, 2, 3] }) def faulty_func(x): return x + 1 result = data.groupby('Group').agg(faulty_func) print(result)
Attempts:
2 left
💡 Hint
Check what the function receives and what operation it tries to do.
✗ Incorrect
The agg function expects the custom function to reduce the group's Series to a scalar value. Here, the function returns x + 1, which is still a Series of the same length, causing ValueError: Function does not reduce.
❓ visualization
advanced2:30remaining
Visualizing group means with a custom function
Which option correctly produces a bar plot of the mean 'Score' per 'Category' using a custom aggregation function?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = pd.DataFrame({ 'Category': ['A', 'A', 'B', 'B', 'C'], 'Score': [10, 20, 30, 40, 50] }) def mean_func(x): return x.mean() result = data.groupby('Category')['Score'].agg(mean_func) result.plot(kind='bar') plt.show()
Attempts:
2 left
💡 Hint
Check the plot kind and aggregation function used.
✗ Incorrect
The code groups by 'Category', calculates mean scores, then plots a bar chart with correct heights for each category.
🚀 Application
expert3:00remaining
Applying multiple custom functions with groupby
Given this DataFrame, which option correctly applies two custom functions to the 'Value' column grouped by 'Category' and returns a DataFrame with both results?
Pandas
import pandas as pd data = pd.DataFrame({ 'Category': ['X', 'X', 'Y', 'Y', 'Z'], 'Value': [1, 3, 5, 7, 9] }) def range_func(x): return x.max() - x.min() def mean_plus_one(x): return x.mean() + 1 result = data.groupby('Category')['Value'].agg([range_func, mean_plus_one]) print(result)
Attempts:
2 left
💡 Hint
Check how agg accepts a list of functions and the output format.
✗ Incorrect
The agg method accepts a list of functions and applies each to the grouped column, returning a DataFrame with columns named after the functions. The calculations match the values shown in option B.