1. Home
  2. Data Science
  3. Pandas
  4. What does df.groupby('k').cumcount() return for each row?

Added 30 Nov -0001

What does df.groupby('k').cumcount() return for each row?

  1. A running count within its own group, starting again at zero per group Correct answer
  2. A running count across the whole frame, ignoring the group boundaries
  3. The total number of rows in its group, broadcast back to every member
  4. A cumulative sum of those counts, so the last row holds the group size

Correct answer

A. A running count within its own group, starting again at zero per group

11 views

MCQSEXAM

Explanation

cumcount numbers the rows inside each group from zero upward, which is the standard way to build an ordinal position per group before pivoting or deduplicating. It differs from ngroup, which labels each group with a distinct integer id, and from transform('size'), which broadcasts the group size. Both cumcount and ngroup return a Series aligned to the original index.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

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