1. Home
  2. Data Science
  3. Pandas
  4. Which groupby.agg form lets you name the output columns at…

Added 30 Nov -0001

Which groupby.agg form lets you name the output columns at the same time as choosing the aggregation?

  1. Calling agg twice, since the second call renames the resulting columns
  2. Named aggregation, with keywords mapping each name to a column and func Correct answer
  3. Passing a dictionary of column names to functions, then renaming afterwards
  4. Passing a list of function names, which yields already-named columns

Correct answer

B. Named aggregation, with keywords mapping each name to a column and func

15 views

MCQSEXAM

Explanation

Named aggregation takes keyword arguments such as total=('sales', 'sum'), so the aggregation and the output names are declared together and the result has a flat column index. The older dict-of-lists form returns MultiIndex columns that then need flattening, and a plain list of functions keeps the source column names. Named aggregation also avoids renaming MultiIndex columns by position, which is fragile.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs