Challenge - 5 Problems
Master of Multiple Aggregations
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple aggregation functions on grouped data
What is the output of this code snippet that groups data by 'Category' and applies multiple aggregation functions on 'Value'?
Pandas
import pandas as pd data = {'Category': ['A', 'A', 'B', 'B', 'C'], 'Value': [10, 20, 10, 30, 50]} df = pd.DataFrame(data) result = df.groupby('Category')['Value'].agg(['sum', 'mean']).reset_index() print(result)
Attempts:
2 left
💡 Hint
Look at how pandas formats the output DataFrame with multiple aggregation functions.
✗ Incorrect
The groupby with agg(['sum', 'mean']) returns a DataFrame with columns 'sum' and 'mean' for each group. The values are numeric with 'sum' as int and 'mean' as float. The reset_index() makes 'Category' a column again.
❓ data_output
intermediate1:30remaining
Number of rows in multi-aggregation result
After running this code, how many rows does the resulting DataFrame have?
Pandas
import pandas as pd data = {'Group': ['X', 'X', 'Y', 'Y', 'Z', 'Z'], 'Score': [5, 10, 15, 20, 25, 30]} df = pd.DataFrame(data) result = df.groupby('Group')['Score'].agg(['min', 'max', 'mean'])
Attempts:
2 left
💡 Hint
Count unique groups in the 'Group' column.
✗ Incorrect
The groupby groups data by unique values in 'Group'. There are 3 unique groups: X, Y, and Z. So the result has 3 rows.
🔧 Debug
advanced2:00remaining
Identify the error in applying multiple aggregation functions
What error does this code raise when trying to apply multiple aggregation functions to a DataFrame column?
Pandas
import pandas as pd data = {'Type': ['A', 'B', 'A'], 'Value': [1, 2, 3]} df = pd.DataFrame(data) result = df.groupby('Type')['Value'].agg('sum', 'mean')
Attempts:
2 left
💡 Hint
Check how agg() accepts multiple functions.
✗ Incorrect
agg() expects a single function, a list of functions, or a dict. Passing multiple positional arguments causes a TypeError.
🚀 Application
advanced2:30remaining
Applying different aggregation functions to different columns
Given this DataFrame, which option correctly applies 'sum' to 'Sales' and 'mean' to 'Quantity' grouped by 'Region'?
Pandas
import pandas as pd data = {'Region': ['East', 'East', 'West', 'West'], 'Sales': [100, 200, 150, 300], 'Quantity': [1, 2, 3, 4]} df = pd.DataFrame(data)
Attempts:
2 left
💡 Hint
Check the dictionary keys and values for agg() to apply different functions per column.
✗ Incorrect
agg() accepts a dictionary mapping column names to aggregation functions. 'Sales' should map to 'sum' and 'Quantity' to 'mean'.
🧠 Conceptual
expert3:00remaining
Understanding output structure of multiple aggregation functions with named aggregation
What is the structure of the DataFrame resulting from this code?
Pandas
import pandas as pd data = {'Category': ['A', 'A', 'B', 'B'], 'Value': [10, 20, 30, 40]} df = pd.DataFrame(data) result = df.groupby('Category').agg(total_value=('Value', 'sum'), average_value=('Value', 'mean'))
Attempts:
2 left
💡 Hint
Named aggregation creates columns with the given names.
✗ Incorrect
Using named aggregation with tuples assigns new column names. The result is a DataFrame indexed by 'Category' with columns 'total_value' and 'average_value'.