polars.DataFrame.sort

DataFrame.sort(by: Union[str, polars.internals.expr.Expr, List[str], List[polars.internals.expr.Expr]], reverse: Union[bool, List[bool]] = False, *, in_place: Literal[False] = False) polars.internals.frame.DataFrame
DataFrame.sort(by: Union[str, polars.internals.expr.Expr, List[str], List[polars.internals.expr.Expr]], reverse: Union[bool, List[bool]] = False, *, in_place: Literal[True]) None
DataFrame.sort(by: Union[str, polars.internals.expr.Expr, List[str], List[polars.internals.expr.Expr]], reverse: Union[bool, List[bool]] = False, *, in_place: bool) Optional[polars.internals.frame.DataFrame]

Sort the DataFrame by column.

Parameters
by

By which column to sort. Only accepts string.

reverse

Reverse/descending sort.

in_place

Perform operation in-place.

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6.0, 7.0, 8.0],
...         "ham": ["a", "b", "c"],
...     }
... )
>>> df.sort("foo", reverse=True)
shape: (3, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
│ --- ┆ --- ┆ --- │
│ i64 ┆ f64 ┆ str │
╞═════╪═════╪═════╡
│ 3   ┆ 8   ┆ c   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 7   ┆ b   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 1   ┆ 6   ┆ a   │
└─────┴─────┴─────┘

Sort by multiple columns. For multiple columns we can also use expression syntax.

>>> df.sort(
...     [pl.col("foo"), pl.col("bar") ** 2],
...     reverse=[True, False],
... )
shape: (3, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
│ --- ┆ --- ┆ --- │
│ i64 ┆ f64 ┆ str │
╞═════╪═════╪═════╡
│ 3   ┆ 64  ┆ c   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 49  ┆ b   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 1   ┆ 36  ┆ a   │
└─────┴─────┴─────┘