A. agg() does not accept multiple functions for different columns.
B. The column names 'Sales' and 'Profit' must be passed as a list, not dict.
C. The code is correct and runs without error.
D. The dictionary mixes single string and list of functions for different columns, which is invalid.
Solution
Step 1: Check agg() dictionary syntax
agg() accepts a dictionary where values can be a string or list of functions per column.
Step 2: Verify code correctness
The code correctly applies 'sum' to 'Sales' and ['mean', 'max'] to 'Profit' without error.
Final Answer:
The code is correct and runs without error. -> Option C
Quick Check:
agg() accepts mixed string and list in dict [OK]
Hint: agg() allows string or list per column in dict [OK]
Common Mistakes:
Thinking mixing string and list causes error
Expecting all functions must be in a list
Confusing agg() with apply() syntax
5. You have a DataFrame df with columns 'Store', 'Sales', and 'Expenses'. You want to group by 'Store' and get the total sales, average expenses, and count of sales entries per store in one step. Which code correctly does this?
hard
A. df.groupby('Store').agg({'Sales': 'sum', 'Expenses': ['mean', 'count']})
B. df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'})
C. df.groupby('Store').agg({'Sales': 'mean', 'Expenses': 'sum', 'Store': 'count'})
D. df.groupby('Store').agg(['sum', 'mean', 'count'])
Solution
Step 1: Identify required aggregations per column
We want total sales (sum), average expenses (mean), and count of sales entries (count).
Step 2: Map functions correctly in agg()
Use a dict: 'Sales' gets ['sum', 'count'], 'Expenses' gets 'mean'. This matches df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'}).
Final Answer:
df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'}) -> Option B
Quick Check:
Dict with list and string per column [OK]
Hint: Use dict with list for multiple funcs per column [OK]
Common Mistakes:
Putting count under wrong column
Using agg() without dict for columns
Applying all functions to all columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.