Skip to main content

csv_pivot

Function csv_pivot 

Source
pub fn csv_pivot(
    table: &CsvTable,
    row_col: &str,
    col_col: &str,
    val_col: &str,
    agg: PivotAgg,
) -> Result<CsvTable, Error>
Expand description

Create a pivot table from table.

  • row_col: column whose distinct values become pivot rows (index).
  • col_col: column whose distinct values become pivot columns.
  • val_col: numeric column to aggregate.
  • agg: aggregation function.

Returns a new CsvTable where the first column is the row index and subsequent columns are the pivot column values.

Missing combinations produce an empty cell.

ยงExample

use oxiphysics_io::csv::{CsvTable, csv_pivot, PivotAgg};

let data = "region,product,sales\nNorth,A,10\nNorth,B,20\nSouth,A,30\nSouth,B,40\n";
let table = CsvTable::from_str(data, ',').unwrap();
let pivot = csv_pivot(&table, "region", "product", "sales", PivotAgg::Sum).unwrap();
assert!(pivot.headers.contains(&"A".to_string()));
assert!(pivot.headers.contains(&"B".to_string()));