table::reshape
On this page
Long <-> wide: pivot spreads one column's values into columns,
melt stacks columns into (variable, value) rows.
Implementations
impl Table
Sourcepub fn pivot(§self, index: List<str>, columns: str, values: str, agg: AggKind) throws (TableError) -> Table One row per distinct
indexkey, one column per distinct value ofcolumns(named by its text, in order of first appearance), each cellaggof thevaluesrows with that pair; null where there are none.nabiuse table::AggKind; let t = table::table!{ "day": ["mon", "mon", "tue"], "city": ["A", "B", "A"], "temp": [20.0, 25.0, 22.0], }; let p = try t.pivot(["day"], "city", "temp", AggKind::Mean) catch _ { t }; assert(p.names() == ["day", "A", "B"]); assert(p.row(1).f64("B") is none);
Sourcepub fn melt(§self, id_vars: List<str>, value_vars: List<str> | none) throws (TableError) -> Table Stack the columns
value_vars(none: every column not inid_vars) into two:variablewith the column name andvaluewith its cell, theid_varsrepeated. Rows go column by column. The value columns need one type (i64withf64givesf64).nabilet t = table::table!{ "id": [1, 2], "a": [10, 20], "b": [30, 40] }; let m = try t.melt(["id"], none) catch _ { t }; assert(m.names() == ["id", "variable", "value"]); assert(m.nrows() == 4 && m.row(2).str("variable") == "b");