polars.Expr.cut
-
Bin continuous values into discrete categories.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
- Parameters:
-
- breaks
-
List of unique cut points.
- labels
-
Names of the categories. The number of labels must be equal to the number of cut points plus one.
- left_closed
-
Set the intervals to be left-closed instead of right-closed.
- include_breaks
-
Include a column with the right endpoint of the bin each observation falls in. This will change the data type of the output from an
Enumto aStruct.
- Returns:
-
- Expr
-
Expression of data type
Enumifinclude_breaksis set toFalse(default), otherwise an expression of data typeStruct.
See also
Examples
Divide a column into three categories.
>>> df = pl.DataFrame({"foo": [-2, -1, 0, 1, 2]}) >>> df.with_columns( ... pl.col("foo").cut([-1, 1], labels=["a", "b", "c"]).alias("cut") ... ) shape: (5, 2) ┌─────┬──────┐ │ foo ┆ cut │ │ --- ┆ --- │ │ i64 ┆ enum │ ╞═════╪══════╡ │ -2 ┆ a │ │ -1 ┆ a │ │ 0 ┆ b │ │ 1 ┆ b │ │ 2 ┆ c │ └─────┴──────┘Add both the category and the breakpoint.
>>> df.with_columns( ... pl.col("foo").cut([-1, 1], include_breaks=True).alias("cut") ... ).unnest("cut") shape: (5, 3) ┌─────┬────────────┬────────────┐ │ foo ┆ breakpoint ┆ category │ │ --- ┆ --- ┆ --- │ │ i64 ┆ f64 ┆ enum │ ╞═════╪════════════╪════════════╡ │ -2 ┆ -1.0 ┆ (-inf, -1] │ │ -1 ┆ -1.0 ┆ (-inf, -1] │ │ 0 ┆ 1.0 ┆ (-1, 1] │ │ 1 ┆ 1.0 ┆ (-1, 1] │ │ 2 ┆ inf ┆ (1, inf] │ └─────┴────────────┴────────────┘
Expr.cut(
breaks: Sequence[float],
*,
labels: Sequence[str_] | None = None,
left_closed: bool = False,
include_breaks: bool = False,
) → Expr
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.cut.html