pub trait UnpivotDF: IntoDf {
// Provided methods
fn unpivot<I, J>(&self, on: I, index: J) -> Result<DataFrame, PolarsError>
where I: IntoVec<PlSmallStr>,
J: IntoVec<PlSmallStr> { ... }
fn unpivot2(&self, args: UnpivotArgsIR) -> Result<DataFrame, PolarsError> { ... }
}
Provided Methods§
Sourcefn unpivot<I, J>(&self, on: I, index: J) -> Result<DataFrame, PolarsError>
fn unpivot<I, J>(&self, on: I, index: J) -> Result<DataFrame, PolarsError>
Unpivot a DataFrame
from wide to long format.
§Example
§Arguments
on
- String slice that represent the columns to use as value variables.index
- String slice that represent the columns to use as id variables.
If on
is empty all columns that are not in index
will be used.
ⓘ
let df = df!("A" => &["a", "b", "a"],
"B" => &[1, 3, 5],
"C" => &[10, 11, 12],
"D" => &[2, 4, 6]
)?;
let unpivoted = df.unpivot(&["A", "B"], &["C", "D"])?;
println!("{:?}", df);
println!("{:?}", unpivoted);
Outputs:
+-----+-----+-----+-----+
| A | B | C | D |
| --- | --- | --- | --- |
| str | i32 | i32 | i32 |
+=====+=====+=====+=====+
| "a" | 1 | 10 | 2 |
+-----+-----+-----+-----+
| "b" | 3 | 11 | 4 |
+-----+-----+-----+-----+
| "a" | 5 | 12 | 6 |
+-----+-----+-----+-----+
+-----+-----+----------+-------+
| A | B | variable | value |
| --- | --- | --- | --- |
| str | i32 | str | i32 |
+=====+=====+==========+=======+
| "a" | 1 | "C" | 10 |
+-----+-----+----------+-------+
| "b" | 3 | "C" | 11 |
+-----+-----+----------+-------+
| "a" | 5 | "C" | 12 |
+-----+-----+----------+-------+
| "a" | 1 | "D" | 2 |
+-----+-----+----------+-------+
| "b" | 3 | "D" | 4 |
+-----+-----+----------+-------+
| "a" | 5 | "D" | 6 |
+-----+-----+----------+-------+
Sourcefn unpivot2(&self, args: UnpivotArgsIR) -> Result<DataFrame, PolarsError>
fn unpivot2(&self, args: UnpivotArgsIR) -> Result<DataFrame, PolarsError>
Similar to unpivot, but without generics. This may be easier if you want to pass
an empty index
or empty on
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.