Challenge - 5 Problems
Advanced Grouping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of groupby with multiple aggregation functions
What is the output of this code snippet using pandas grouping and aggregation?
Pandas
import pandas as pd data = {'Team': ['A', 'A', 'B', 'B', 'C', 'C'], 'Points': [10, 15, 10, 20, 10, 30], 'Assists': [5, 7, 8, 6, 7, 9]} df = pd.DataFrame(data) result = df.groupby('Team').agg({'Points': ['sum', 'mean'], 'Assists': 'max'}) print(result)
Attempts:
2 left
💡 Hint
Look carefully at the aggregation functions applied to each column.
✗ Incorrect
The code groups by 'Team' and calculates sum and mean for 'Points' and max for 'Assists'. The max values for 'Assists' are the highest in each group.
❓ data_output
intermediate1:30remaining
Number of groups after grouping by multiple columns
Given this DataFrame, how many groups will result from grouping by both 'Category' and 'Subcategory'?
Pandas
import pandas as pd data = {'Category': ['Fruit', 'Fruit', 'Vegetable', 'Vegetable', 'Fruit'], 'Subcategory': ['Citrus', 'Berry', 'Root', 'Leafy', 'Berry'], 'Quantity': [10, 15, 7, 8, 5]} df = pd.DataFrame(data) groups = df.groupby(['Category', 'Subcategory']) print(len(groups))
Attempts:
2 left
💡 Hint
Count unique pairs of Category and Subcategory.
✗ Incorrect
There are 4 unique pairs: (Fruit, Citrus), (Fruit, Berry), (Vegetable, Root), (Vegetable, Leafy).
🔧 Debug
advanced2:00remaining
Identify the error in this advanced grouping code
What error does this code raise when trying to group and filter a DataFrame?
Pandas
import pandas as pd data = {'Team': ['X', 'X', 'Y', 'Y'], 'Score': [10, 20, 15, 25]} df = pd.DataFrame(data) filtered = df.groupby('Team').filter(lambda x: x['Score'].mean() > 15) print(filtered)
Attempts:
2 left
💡 Hint
Check how groupby filter works with lambda functions.
✗ Incorrect
The code correctly filters groups where the mean Score is greater than 15, so no error occurs.
❓ visualization
advanced1:30remaining
Interpreting a grouped bar chart from grouped data
You have grouped sales data by 'Region' and 'Product' and plotted a grouped bar chart showing total sales. Which statement best describes what the chart shows?
Attempts:
2 left
💡 Hint
Grouped bar charts display categories side by side for comparison.
✗ Incorrect
Grouped bar charts place bars for each product side by side within each region group, showing total sales per product per region.
🚀 Application
expert2:30remaining
Choosing the right grouping method for time series data
You have a DataFrame with timestamps and sales data. You want to analyze weekly sales totals. Which pandas method and parameters will correctly group the data by week?
Pandas
import pandas as pd # df has columns 'Date' (datetime) and 'Sales' # Which code correctly groups by week and sums sales?
Attempts:
2 left
💡 Hint
Use pd.Grouper with freq='W' to group by week on a datetime column.
✗ Incorrect
Option C uses pd.Grouper with key='Date' and freq='W' to group by week and sum sales correctly. Other options misuse resample or groupby order.