What does groupby(...).apply(f) do with the grouping columns when f returns a DataFrame in pandas 2.x?
Correct answer
D. It prepends the group keys to the result index, so include_groups=False changes the output
13 views
D. It prepends the group keys to the result index, so include_groups=False changes the output
13 views
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.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is D? Ask here and get an explanation.