What does df.groupby('k').cumcount() return for each row?
Correct answer
A. A running count within its own group, starting again at zero per group
11 views
A. A running count within its own group, starting again at zero per group
11 views
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.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is A? Ask here and get an explanation.