0

Consider the following code:

import polars as pl
R=pl.DataFrame({"R":["r1","r2","r3"]})
C=pl.DataFrame({"C":["c1","c2",]})
RC=R.join(C,how="cross")
RC=RC.with_columns(pl.Series(name="VAL",values=range(1,7)))
print(RC)

shape: (6, 3)
┌─────┬─────┬─────┐
│ R   ┆ C   ┆ VAL │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ i64 │
╞═════╪═════╪═════╡
│ r1  ┆ c1  ┆ 1   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r1  ┆ c2  ┆ 2   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r2  ┆ c1  ┆ 3   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r2  ┆ c2  ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r3  ┆ c1  ┆ 5   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r3  ┆ c2  ┆ 6   │
└─────┴─────┴─────┘

RC2=pl.DataFrame({"R":["r1","r2","r3"],"c1":[1,3,5],"c2":[2,4,6]})
print(RC2)

shape: (3, 3)
┌─────┬─────┬─────┐
│ R   ┆ c1  ┆ c2  │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ r1  ┆ 1   ┆ 2   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r2  ┆ 3   ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ r3  ┆ 5   ┆ 6   │
└─────┴─────┴─────┘

what is the best way to transform RC in RC2 ? I was thinking about a a series of groupby operations but there should be a better solution... and from RC2 back to RC? Thanks

1 Answers1

0

You can use the pandas pivot method

RC2 = RC.pivot(values='VAL', index='R', columns='C')

Results:

shape: (3, 3)
┌─────┬─────┬─────┐
│ R   ┆ c1  ┆ c2  │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ r1  ┆ 1   ┆ 2   │
│ r2  ┆ 3   ┆ 4   │
│ r3  ┆ 5   ┆ 6   │
└─────┴─────┴─────┘

Now turning RC2 back to RC exactly is not really an option as we've lost the order, but I would suggest reading about the pandas melt method

CodeCop
  • 1
  • 2
  • 15
  • 37