row_number

pydiverse.transform.row_number(
*,
partition_by: Col | ColName | str | Iterable[Col | ColName | str] | None = None,
arrange: ColExpr | Iterable[ColExpr] | None = None,
) ColExpr[Int][source]

Computes the index of a row.

Via the arrange argument, this can be done relative to a different order of the rows. But note that the result may not be unique if the argument of arrange contains duplicates.

Examples

>>> t = pdt.Table({"a": [5, -1, 435, -34, 8, None, 0]})
>>> (
...     t
...     >> mutate(
...         x=pdt.row_number(),
...         y=pdt.row_number(arrange=t.a),
...     )
...     >> show()
... )
Table <unnamed>, backend: PolarsImpl
shape: (7, 3)
┌──────┬─────┬─────┐
│ a    ┆ x   ┆ y   │
│ ---  ┆ --- ┆ --- │
│ i64  ┆ i64 ┆ i64 │
╞══════╪═════╪═════╡
│ 5    ┆ 1   ┆ 5   │
│ -1   ┆ 2   ┆ 3   │
│ 435  ┆ 3   ┆ 7   │
│ -34  ┆ 4   ┆ 2   │
│ 8    ┆ 5   ┆ 6   │
│ null ┆ 6   ┆ 1   │
│ 0    ┆ 7   ┆ 4   │
└──────┴─────┴─────┘