pub trait DataFrameOps: IntoDf {
// Provided methods
fn to_dummies(
&self,
separator: Option<&str>,
drop_first: bool,
) -> Result<DataFrame, PolarsError> { ... }
fn columns_to_dummies(
&self,
columns: Vec<&str>,
separator: Option<&str>,
drop_first: bool,
) -> Result<DataFrame, PolarsError> { ... }
fn _to_dummies(
&self,
columns: Option<Vec<&str>>,
separator: Option<&str>,
drop_first: bool,
) -> Result<DataFrame, PolarsError> { ... }
}
Available on crate feature
polars-ops
only.Provided Methods§
Sourcefn to_dummies(
&self,
separator: Option<&str>,
drop_first: bool,
) -> Result<DataFrame, PolarsError>
Available on crate feature to_dummies
only.
fn to_dummies( &self, separator: Option<&str>, drop_first: bool, ) -> Result<DataFrame, PolarsError>
to_dummies
only.Create dummy variables.
§Example
ⓘ
use polars_core::prelude::*;
let df = df! {
"id" => &[1, 2, 3, 1, 2, 3, 1, 1],
"type" => &["A", "B", "B", "B", "C", "C", "C", "B"],
"code" => &["X1", "X2", "X3", "X3", "X2", "X2", "X1", "X1"]
}.unwrap();
let dummies = df.to_dummies(None, false).unwrap();
println!("{}", dummies);
Outputs:
+------+------+------+--------+--------+--------+---------+---------+---------+
| id_1 | id_3 | id_2 | type_A | type_B | type_C | code_X1 | code_X2 | code_X3 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| u8 | u8 | u8 | u8 | u8 | u8 | u8 | u8 | u8 |
+======+======+======+========+========+========+=========+=========+=========+
| 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 0 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 0 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
+------+------+------+--------+--------+--------+---------+---------+---------+
| 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |
+------+------+------+--------+--------+--------+---------+---------+---------+
fn columns_to_dummies( &self, columns: Vec<&str>, separator: Option<&str>, drop_first: bool, ) -> Result<DataFrame, PolarsError>
Available on crate feature
to_dummies
only.fn _to_dummies( &self, columns: Option<Vec<&str>>, separator: Option<&str>, drop_first: bool, ) -> Result<DataFrame, PolarsError>
Available on crate feature
to_dummies
only.