datatable.ngroup()

Added in version 1.1.0

For each row return a group number it belongs to. 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, groups are numbered in the ascending order. Otherwise, when this parameter is True, groups are numbered in the descending order.

return
FExpr

f-expression that returns group numbers for each row.

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 groups in the ascending order:

DT[:, dt.ngroup(), f.C0]
C0C1
str32int64
0a0
1a0
2a0
3b1
4b1
5c2
6c2
7c2

Number groups in the descending order:

DT[:, dt.ngroup(reverse = True), f.C0]
C0C1
str32int64
0a2
1a2
2a2
3b1
4b1
5c0
6c0
7c0

Number groups in the absence of by():

DT[:, dt.ngroup()]
C0C1
str32int64
0a0
1a0
2a0
3b0
4b0
5c0
6c0
7c0