polars.Series.to_dummies
-
Get dummy/indicator variables.
- Parameters:
-
- separator
-
Separator/delimiter used when generating column names.
- drop_first
-
Remove the first category from the variable being encoded.
- drop_nulls
-
If there are
Nonevalues in the series, anullcolumn is not generated. Null values in the input are represented by zero vectors.
Examples
>>> s = pl.Series("a", [1, 2, 3]) >>> s.to_dummies() shape: (3, 3) ┌─────┬─────┬─────┐ │ a_1 ┆ a_2 ┆ a_3 │ │ --- ┆ --- ┆ --- │ │ u8 ┆ u8 ┆ u8 │ ╞═════╪═════╪═════╡ │ 1 ┆ 0 ┆ 0 │ │ 0 ┆ 1 ┆ 0 │ │ 0 ┆ 0 ┆ 1 │ └─────┴─────┴─────┘>>> s.to_dummies(drop_first=True) shape: (3, 2) ┌─────┬─────┐ │ a_2 ┆ a_3 │ │ --- ┆ --- │ │ u8 ┆ u8 │ ╞═════╪═════╡ │ 0 ┆ 0 │ │ 1 ┆ 0 │ │ 0 ┆ 1 │ └─────┴─────┘
>>> s = pl.Series("a", [1, 2, None, 3]) >>> s.to_dummies(drop_nulls=True, drop_first=True) shape: (4, 2) ┌─────┬─────┐ │ a_2 ┆ a_3 │ │ --- ┆ --- │ │ u8 ┆ u8 │ ╞═════╪═════╡ │ 0 ┆ 0 │ │ 1 ┆ 0 │ │ 0 ┆ 0 │ │ 0 ┆ 1 │ └─────┴─────┘
Series.to_dummies(
*,
separator: str_ = '_',
drop_first: bool = False,
drop_nulls: bool = False,
) → DataFrame
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/series/api/polars.Series.to_dummies.html