Challenge - 5 Problems
Named Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of named aggregation with multiple functions
What is the output DataFrame after running this code with named aggregation?
Pandas
import pandas as pd df = pd.DataFrame({ 'Team': ['A', 'A', 'B', 'B', 'C'], 'Points': [10, 15, 10, 20, 30], 'Assists': [5, 7, 8, 6, 10] }) result = df.groupby('Team').agg( total_points=pd.NamedAgg(column='Points', aggfunc='sum'), max_assists=pd.NamedAgg(column='Assists', aggfunc='max') ) print(result)
Attempts:
2 left
💡 Hint
Look at how named aggregation creates new columns with the given names.
✗ Incorrect
The groupby aggregates sum of Points as total_points and max of Assists as max_assists for each Team. The output is a DataFrame indexed by Team with these two columns.
❓ data_output
intermediate1:30remaining
Number of rows in named aggregation result
Given this DataFrame and named aggregation, how many rows does the result have?
Pandas
import pandas as pd df = pd.DataFrame({ 'Category': ['X', 'X', 'Y', 'Z', 'Z', 'Z'], 'Value': [1, 2, 3, 4, 5, 6] }) result = df.groupby('Category').agg( sum_value=pd.NamedAgg(column='Value', aggfunc='sum'), count_value=pd.NamedAgg(column='Value', aggfunc='count') ) print(len(result))
Attempts:
2 left
💡 Hint
Count unique groups in the 'Category' column.
✗ Incorrect
The groupby groups by unique categories: X, Y, Z, so 3 groups and 3 rows in result.
🔧 Debug
advanced2:00remaining
Identify the error in named aggregation syntax
What error does this code raise?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1,2,3], 'B': [4,5,6]}) result = df.groupby('A').agg( total_B=('B', 'sum'), mean_B='mean' ) print(result)
Attempts:
2 left
💡 Hint
Check how named aggregation arguments are passed.
✗ Incorrect
Named aggregation requires all aggregations as keyword arguments with pd.NamedAgg or tuples. Mixing tuple and string without keyword causes TypeError.
🚀 Application
advanced2:30remaining
Create a named aggregation to find min and max
Which option correctly uses named aggregation to find min and max of 'Score' grouped by 'Group'?
Pandas
import pandas as pd df = pd.DataFrame({ 'Group': ['G1', 'G1', 'G2', 'G2'], 'Score': [88, 92, 85, 90] }) result = df.groupby('Group').agg( min_score=..., # fill here max_score=... # fill here ) print(result)
Attempts:
2 left
💡 Hint
Named aggregation uses pd.NamedAgg or keyword tuples.
✗ Incorrect
Option D correctly uses pd.NamedAgg with column and aggfunc keywords for named aggregation.
🧠 Conceptual
expert3:00remaining
Understanding output structure of named aggregation
After applying named aggregation with multiple functions on different columns, what is the structure of the resulting DataFrame?
Attempts:
2 left
💡 Hint
Named aggregation creates new columns with given names and keeps group keys as index.
✗ Incorrect
Named aggregation returns a DataFrame indexed by groups with columns named as specified, each column showing the aggregated result.