0
0
Pandasdata~20 mins

Named aggregation in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A{'Team': ['A', 'B', 'C'], 'total_points': [25, 30, 30], 'max_assists': [7, 8, 10]}
B
       total_points  max_assists
Team                            
A                25            7
B                30            8
C                30           10
C
Team
A    total_points    25
     max_assists      7
B    total_points    30
     max_assists      8
C    total_points    30
     max_assists     10
dtype: int64
D
       Points  Assists
Team                  
A          25       12
B          30       14
C          30       10
Attempts:
2 left
💡 Hint
Look at how named aggregation creates new columns with the given names.
data_output
intermediate
1: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))
A3
B6
C2
D1
Attempts:
2 left
💡 Hint
Count unique groups in the 'Category' column.
🔧 Debug
advanced
2: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)
ATypeError: named aggregation expects keyword arguments
BSyntaxError: invalid syntax
CKeyError: 'mean_B'
DNo error, prints grouped DataFrame
Attempts:
2 left
💡 Hint
Check how named aggregation arguments are passed.
🚀 Application
advanced
2: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)
A{'min_score': ('Score', 'min'), 'max_score': ('Score', 'max')}
Bmin_score=('Score', 'min'), max_score=('Score', 'max')
Cmin_score='min', max_score='max'
Dmin_score=pd.NamedAgg(column='Score', aggfunc='min'), max_score=pd.NamedAgg(column='Score', aggfunc='max')
Attempts:
2 left
💡 Hint
Named aggregation uses pd.NamedAgg or keyword tuples.
🧠 Conceptual
expert
3: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?
AA MultiIndex DataFrame with original column names as first level and aggregation functions as second level.
BA Series with group keys as index and tuples of aggregated values as elements.
CA DataFrame indexed by group keys with columns named as specified in named aggregation, each column holding aggregated values.
DA DataFrame with original columns replaced by aggregated columns without group keys as index.
Attempts:
2 left
💡 Hint
Named aggregation creates new columns with given names and keeps group keys as index.