Hint: Sum points after groupby sums values per group [OK]
Common Mistakes:
Summing only first value per group
Confusing sum with mean
Expecting agg() needs list always
4. You run this code:
df.groupby('Category').agg('sum', 'Price')
But get an error. What is the problem?
medium
A. agg() expects a dictionary or list, not multiple arguments
B. groupby() requires numeric columns only
C. sum is not a valid aggregation function
D. Price column does not exist
Solution
Step 1: Check agg() function signature
The agg() method expects a single function, list of functions, or dict mapping columns to functions.
Step 2: Identify incorrect argument usage
Passing two separate arguments ('sum', 'Price') is invalid syntax and causes error.
Final Answer:
agg() expects a dictionary or list, not multiple arguments -> Option A
Quick Check:
agg() needs one argument: function or dict [OK]
Hint: Use agg({'Price': 'sum'}) or agg('sum') only [OK]
Common Mistakes:
Passing multiple arguments to agg()
Assuming groupby needs numeric columns only
Using invalid function names
5. You have a sales dataframe with columns 'Store', 'Product', and 'Revenue'. You want to find the total revenue per store and the average revenue per product within each store. Which pandas code correctly achieves this?
hard
A. df.groupby('Store').agg({'Revenue': ['sum', 'mean']})
B. df.groupby(['Store', 'Product']).agg({'Revenue': 'mean'}).groupby('Store').agg({'Revenue': 'sum'})
C. df.groupby('Store').agg({'Revenue': 'sum'}).groupby('Product').agg({'Revenue': 'mean'})
D. df.groupby('Product').agg({'Revenue': ['sum', 'mean']})
Solution
Step 1: Calculate average revenue per product within each store
Group by both 'Store' and 'Product', then calculate mean revenue.
Step 2: Calculate total revenue per store
Group the result by 'Store' and sum the mean revenues of products.
Final Answer:
df.groupby(['Store', 'Product']).agg({'Revenue': 'mean'}).groupby('Store').agg({'Revenue': 'sum'}) -> Option B
Quick Check:
Nested groupby for product mean then store sum [OK]
Hint: Group by both, then aggregate stepwise for nested summaries [OK]
Common Mistakes:
Trying to do sum and mean in one groupby without nesting
Grouping by wrong columns order
Mixing aggregation levels incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.