0
0
Data Analysis Pythondata~20 mins

agg() for multiple aggregations in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
agg() Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiple aggregations with agg()
What is the output of this code snippet using pandas agg() for multiple aggregations?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8]
})

result = df.agg({'A': ['sum', 'max'], 'B': ['min', 'mean']})
print(result)
A{'A': {'sum': 10, 'max': 4}, 'B': {'min': 5, 'mean': 6.5}}
B
       A    B
sum  10.0  26.0
max   4.0   8.0
C
       A    B
sum  10.0  NaN
max   4.0  NaN
min   NaN  5.0
mean  NaN  6.5
D
A    10.0
B     5.0
dtype: float64
Attempts:
2 left
💡 Hint
agg() with dict applies each function per column and returns a DataFrame with function names as index.
data_output
intermediate
1:30remaining
Number of rows in agg() result with multiple aggregations
Given this DataFrame and agg() call, how many rows does the result have?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20, 30],
    'Y': [5, 15, 25]
})

result = df.agg({'X': ['min', 'max', 'mean'], 'Y': ['sum', 'mean']})
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
Count unique aggregation functions used across all columns.
🔧 Debug
advanced
2:00remaining
Identify the error in agg() usage
What error does this code raise when using agg() with multiple aggregations?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})

result = df.agg({'A': 'sum', 'B': ['min', 'max']})
ATypeError: unhashable type: 'list'
BAttributeError: 'list' object has no attribute 'agg'
CValueError: No axis named B for object type DataFrame
DKeyError: 'B'
Attempts:
2 left
💡 Hint
Check if all columns in agg dict exist in DataFrame.
🚀 Application
advanced
2:30remaining
Using agg() to summarize grouped data
You have this DataFrame with groups. Which option shows the correct agg() output for multiple aggregations per group?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Group': ['X', 'X', 'Y', 'Y'],
    'Value': [10, 20, 30, 40]
})

grouped = df.groupby('Group').agg({'Value': ['min', 'max', 'mean']})
print(grouped)
A
       Value
min  max  mean
Group               
X    10   20  15.0
Y    30   40  35.0
B
       Value
min max mean
Group               
X   10  20  15.0
Y   30  40  35.0
C
       Value
        min max mean
Group               
X         10  20  15.0
Y         30  40  35.0
D
       Value
min max mean
Group               
X  10  20  15.0
Y  30  40  35.0
Attempts:
2 left
💡 Hint
Check the multi-level column index format pandas produces for multiple aggregations.
🧠 Conceptual
expert
2:30remaining
Understanding agg() output shape with overlapping functions
If you run agg() on a DataFrame with columns 'A' and 'B' using {'A': ['sum', 'mean'], 'B': ['mean', 'max']}, how many rows and columns will the output DataFrame have?
A4 rows and 4 columns
B3 rows and 2 columns
C2 rows and 4 columns
D2 rows and 2 columns
Attempts:
2 left
💡 Hint
Rows correspond to unique aggregation functions, columns to original columns.