datatable.cumcount()

Added in version 1.1.0

Number rows within each group. In the absence of by() the frame is assumed to consist of one group only.

Parameters

reverse
bool

By default, when this parameter is False, the numbering is performed in the ascending order. Otherwise, when this parameter is True, the numbering is done in the descending order.

return
FExpr

f-expression that returns row numbers within each group.

Examples

Create a sample datatable frame:

from datatable import dt, f, by DT = dt.Frame(['a', 'a', 'a', 'b', 'b', 'c', 'c', 'c']) DT
C0
str32
0a
1a
2a
3b
4b
5c
6c
7c

Number rows within each group in the ascending order:

DT[:, dt.cumcount(), f.C0]
C0C1
str32int64
0a0
1a1
2a2
3b0
4b1
5c0
6c1
7c2

Number rows within each group in the descending order:

DT[:, dt.cumcount(reverse = True), f.C0]
C0C1
str32int64
0a2
1a1
2a0
3b1
4b0
5c2
6c1
7c0

Number rows in the absence of by():

DT[:, [f.C0, dt.cumcount()]]
C0C1
str32int64
0a0
1a1
2a2
3b3
4b4
5c5
6c6
7c7