polars.Expr.rank¶
- Expr.rank(method: str = 'average', reverse: bool = False) polars.internals.expr.Expr ¶
Assign ranks to data, dealing with ties appropriately.
- Parameters
- method
{‘average’, ‘min’, ‘max’, ‘dense’, ‘ordinal’, ‘random’}, optional The method used to assign ranks to tied elements. The following methods are available (default is ‘average’): - ‘average’: The average of the ranks that would have been assigned to all the tied values is assigned to each value. - ‘min’: The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as “competition” ranking.) - ‘max’: The maximum of the ranks that would have been assigned to all the tied values is assigned to each value. - ‘dense’: Like ‘min’, but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements. - ‘ordinal’: All values are given a distinct rank, corresponding to the order that the values occur in a. - ‘random’: Like ‘ordinal’, but the rank for ties is not dependent on the order that the values occur in a.
- reverse
reverse the operation
Examples
>>> df = pl.DataFrame({"a": [0, 1, 2, 2, 4]}) >>> df.select(pl.col("a").rank()) shape: (5, 1) ┌─────┐ │ a │ │ --- │ │ f32 │ ╞═════╡ │ 1.0 │ ├╌╌╌╌╌┤ │ 2.0 │ ├╌╌╌╌╌┤ │ 3.5 │ ├╌╌╌╌╌┤ │ 3.5 │ ├╌╌╌╌╌┤ │ 5.0 │ └─────┘