polars::prelude

Trait DataFrameOps

Source
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§

Source

fn to_dummies( &self, separator: Option<&str>, drop_first: bool, ) -> Result<DataFrame, PolarsError>

Available on crate feature 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       |
 +------+------+------+--------+--------+--------+---------+---------+---------+
Source

fn columns_to_dummies( &self, columns: Vec<&str>, separator: Option<&str>, drop_first: bool, ) -> Result<DataFrame, PolarsError>

Available on crate feature to_dummies only.
Source

fn _to_dummies( &self, columns: Option<Vec<&str>>, separator: Option<&str>, drop_first: bool, ) -> Result<DataFrame, PolarsError>

Available on crate feature to_dummies only.

Implementors§

Source§

impl<T> DataFrameOps for T
where T: IntoDf,