1. Home
  2. Data Science
  3. Pandas
  4. What does groupby(...).apply(f) do with the grouping…

Added 30 Nov -0001

What does groupby(...).apply(f) do with the grouping columns when f returns a DataFrame in pandas 2.x?

  1. It drops the grouping columns entirely, since apply only ever sees the remaining value columns
  2. It raises an error, because apply callbacks returning DataFrames are no longer supported
  3. It duplicates the keys as columns and as index levels, producing redundant result fields
  4. It prepends the group keys to the result index, so include_groups=False changes the output Correct answer

Correct answer

D. It prepends the group keys to the result index, so include_groups=False changes the output

13 views

MCQSEXAM

Explanation

groupby.apply historically passed the grouping columns to the callback and then prepended the group keys as index levels, so keys could appear twice in the result. pandas 2.2 added include_groups=False to suppress that column passing, and later versions warn about the inconsistency. transform is the better tool when the result must align with the original index.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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