0
0
Pandasdata~20 mins

Multiple aggregation functions in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Multiple Aggregations
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[['Category', 'sum', 'mean'], ['A', 30, 15.0], ['B', 40, 20.0], ['C', 50, 50.0]]
B
  Category  Value
0        A  30
1        B  40
2        C  50
C
  Category  sum  mean
0        A  30.0  15
1        B  40.0  20
2        C  50.0  50
D
  Category  sum  mean
0        A   30  15.0
1        B   40  20.0
2        C   50  50.0
Attempts:
2 left
💡 Hint
Look at how pandas formats the output DataFrame with multiple aggregation functions.
data_output
intermediate
1: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'])
A3
B6
C1
D9
Attempts:
2 left
💡 Hint
Count unique groups in the 'Group' column.
🔧 Debug
advanced
2: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')
ANo error, returns sum and mean aggregation
BSyntaxError: invalid syntax
CTypeError: agg() takes from 1 to 2 positional arguments but 3 were given
DValueError: Function names must be strings or list of strings
Attempts:
2 left
💡 Hint
Check how agg() accepts multiple functions.
🚀 Application
advanced
2: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)
Adf.groupby('Region').agg({'Sales': ['sum'], 'Quantity': ['mean']})
Bdf.groupby('Region').agg({'Sales': 'sum', 'Quantity': 'mean'})
Cdf.groupby('Region').agg({'Sales': ['mean'], 'Quantity': ['sum']})
Ddf.groupby('Region').agg({'Sales': 'mean', 'Quantity': 'sum'})
Attempts:
2 left
💡 Hint
Check the dictionary keys and values for agg() to apply different functions per column.
🧠 Conceptual
expert
3: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'))
AA DataFrame with columns 'total_value' and 'average_value' indexed by 'Category'
BA DataFrame with multi-level columns where 'Value' is the top level and 'sum' and 'mean' are sub-levels
CA Series with a MultiIndex of 'Category' and aggregation function names
DA DataFrame with columns 'Value_sum' and 'Value_mean' indexed by integers
Attempts:
2 left
💡 Hint
Named aggregation creates columns with the given names.