Challenge - 5 Problems
agg() Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
agg() with dict applies each function per column and returns a DataFrame with function names as index.
✗ Incorrect
The agg() method applies multiple aggregation functions per column. The result is a DataFrame where rows are the aggregation functions and columns are the original columns. Functions not applied to a column show NaN.
❓ data_output
intermediate1: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']})
Attempts:
2 left
💡 Hint
Count unique aggregation functions used across all columns.
✗ Incorrect
The agg() result has one row per unique aggregation function name used. Here, functions are 'min', 'max', 'mean', 'sum'. That's 4 rows.
🔧 Debug
advanced2: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']})
Attempts:
2 left
💡 Hint
Check if all columns in agg dict exist in DataFrame.
✗ Incorrect
The DataFrame has no column 'B', so trying to aggregate on 'B' raises a KeyError.
🚀 Application
advanced2: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)
Attempts:
2 left
💡 Hint
Check the multi-level column index format pandas produces for multiple aggregations.
✗ Incorrect
When grouping and aggregating multiple functions, pandas creates a multi-level column index with the original column name and aggregation function names.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Rows correspond to unique aggregation functions, columns to original columns.
✗ Incorrect
The output DataFrame has one row per unique aggregation function: 'sum', 'mean', 'max' → 3 unique functions. But since 'sum' applies only to 'A', 'max' only to 'B', the result shows NaN where function not applied. The index will have 3 rows, columns 2.