polars.internals.expr.ExprCatNameSpace.set_ordering

ExprCatNameSpace.set_ordering(ordering: str) polars.internals.expr.Expr

Determine how this categorical series should be sorted.

Parameters
ordering
One of:
  • ‘physical’ -> use the physical representation of the categories to determine the order (default)

  • ‘lexical’ -. use the string values to determine the ordering

Examples

>>> df = pl.DataFrame(
...     {"cats": ["z", "z", "k", "a", "b"], "vals": [3, 1, 2, 2, 3]}
... ).with_columns(
...     [
...         pl.col("cats").cast(pl.Categorical).cat.set_ordering("lexical"),
...     ]
... )
>>> df.sort(["cats", "vals"])
shape: (5, 2)
┌──────┬──────┐
│ cats ┆ vals │
│ ---  ┆ ---  │
│ cat  ┆ i64  │
╞══════╪══════╡
│ a    ┆ 2    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ b    ┆ 3    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ k    ┆ 2    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ z    ┆ 1    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ z    ┆ 3    │
└──────┴──────┘