rank

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

The number of strictly smaller elements in the column plus one.

This is the same as rank("min") in polars. This function has two syntax alternatives, as shown in the example below. The pdt. version is a bit more flexible, because it allows sorting by multiple expressions.

Examples

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