1

How do I create a pivoted dataframe with polars (in Rust), where I can both specify indices and columns?

For indices I want the groups and for the columns I want aggregated calculations per month, and year.

like this, ish:

df = pd.DataFrame({
    "obj" : ["ring", "shoe", "ring"],
    "price":["65", "42", "65"],
    "value":["53", "55", "54"],
    "date":["2022-02-07", "2022-01-07", "2022-03-07"]
})
df_pivo = pl.pivot_table(
    df, index=["obj"], columns=["date"], values=["price","value"], aggfunc=np.sum)
E_net4
  • 27,810
  • 13
  • 101
  • 139
Ynax
  • 59
  • 4

1 Answers1

2

I am not sure if there is an "official" way to do this since pivoting is such a common operation, but this is the way that I found digging in a bit into the tests of the Polars (I am new at it as well)... Maybe someone from Polars can help out here.

I have written in rust "as is" (the desired columns, index and values) in your Python code. You can pivot this way, but if your intention is to pivot to summarize you might be better off with groupby functions (see the examples book https://pola-rs.github.io/polars-book/user-guide/)

You need to use the external crate polars-ops

use polars_ops::pivot::{pivot, PivotAgg};
use polars::prelude::*;

fn main() {
    
    let df = df! [
        "obj"        =>  ["ring", "shoe", "ring"],
        "price"   => [65, 42, 65],
        "value" => [53, 55, 54],
        "date"        =>["2022-02-07", "2022-01-07", "2022-03-07"]
    ]
    .unwrap();

    let out = pivot(&df, ["price", "value"], ["obj"], ["date"], PivotAgg::Sum, true).unwrap();

    println!("{}",df);
    println!("{}",out);
}

Result

shape: (3, 4)
┌──────┬───────┬───────┬────────────┐
│ obj  ┆ price ┆ value ┆ date       │
│ ---  ┆ ---   ┆ ---   ┆ ---        │
│ str  ┆ i32   ┆ i32   ┆ str        │
╞══════╪═══════╪═══════╪════════════╡
│ ring ┆ 65    ┆ 53    ┆ 2022-02-07 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ shoe ┆ 42    ┆ 55    ┆ 2022-01-07 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ ring ┆ 65    ┆ 54    ┆ 2022-03-07 │
└──────┴───────┴───────┴────────────┘

shape: (2, 7)
┌──────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┐
│ obj  ┆ price_2022-01-07 ┆ price_2022-02-07 ┆ price_2022-03-07 ┆ value_2022-01-07 ┆ value_2022-02-07 ┆ value_2022-03-07 │
│ ---  ┆ ---              ┆ ---              ┆ ---              ┆ ---              ┆ ---              ┆ ---              │
│ str  ┆ i32              ┆ i32              ┆ i32              ┆ i32              ┆ i32              ┆ i32              │
╞══════╪══════════════════╪══════════════════╪══════════════════╪══════════════════╪══════════════════╪══════════════════╡
│ ring ┆ null             ┆ 65               ┆ 65               ┆ null             ┆ 53               ┆ 54               │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ shoe ┆ 42               ┆ null             ┆ null             ┆ 55               ┆ null             ┆ null             │
└──────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┘
  • this looks promising, didn't know `polar_ops` and in extension `pivot`existed! This will help, thanks! – Ynax Dec 21 '22 at 20:20
  • 1
    Nice, but it would be nice if time was a visible dimension on the left side. You have a plus from me – Grzesiek Mar 05 '23 at 08:27
  • Thank you. Not sure if polars current version (v0.28.0) has changed since your comment, but I had to change your pivot() code to this to get it to work on my end: ```let out = pivot(&df, ["price", "value"], ["obj"], ["date"], true, Some(PivotAgg::Sum), None).unwrap();``` – Beauregard D Apr 30 '23 at 22:14