Challenge - 5 Problems
Aggregation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of agg() with multiple functions
What is the output of the following code snippet using pandas agg() on a DataFrame?
Pandas
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
Remember that agg() returns a DataFrame with the aggregation functions as the index and columns as the original DataFrame columns.
✗ Incorrect
The agg() method applies specified aggregation functions to each column. Here, 'A' gets 'sum' and 'max', 'B' gets 'min' and 'mean'. The result is a DataFrame with these functions as the index and columns 'A' and 'B'. Missing values appear as NaN where functions are not applied.
❓ data_output
intermediate1:30remaining
Number of rows after groupby and agg
Given the DataFrame and code below, how many rows does the resulting DataFrame have?
Pandas
import pandas as pd df = pd.DataFrame({ 'Category': ['X', 'Y', 'X', 'Z', 'Y', 'Z'], 'Value': [10, 20, 30, 40, 50, 60] }) result = df.groupby('Category').agg({'Value': 'sum'}) print(len(result))
Attempts:
2 left
💡 Hint
Groupby groups rows by unique values in 'Category'. Count unique categories.
✗ Incorrect
The groupby groups rows by unique 'Category' values: 'X', 'Y', 'Z'. There are 3 unique categories, so the resulting DataFrame has 3 rows.
🔧 Debug
advanced1:30remaining
Identify the error in agg() usage
What error does the following code raise when run?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) result = df.agg({'A': 'sum', 'B': 'mean'})
Attempts:
2 left
💡 Hint
Check if all keys in the agg dictionary exist as columns in the DataFrame.
✗ Incorrect
The DataFrame has only column 'A'. Specifying 'B' in agg() causes a KeyError because 'B' does not exist.
🚀 Application
advanced2:00remaining
Using agg() with custom functions
Which option correctly uses agg() to compute the range (max - min) of column 'Scores' in the DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({'Scores': [88, 92, 79, 93, 85]})
Attempts:
2 left
💡 Hint
Range is max value minus min value.
✗ Incorrect
Option C correctly defines a lambda function that calculates the range by subtracting min from max.
🧠 Conceptual
expert2:00remaining
Understanding agg() output shape with mixed aggregations
If you run agg() on a DataFrame with two columns, applying ['sum', 'mean'] to the first column and 'max' to the second column, what will be the shape of the resulting DataFrame?
Attempts:
2 left
💡 Hint
Count total aggregation functions applied and number of columns.
✗ Incorrect
The first column has 2 functions, the second has 1 function, total 3 rows. Columns remain 2. So shape is (3, 2).