python-polars

How to convert rows into columns given an arbitrary step size in polars (in a single step)?


I've managed to solve this problem in two steps.

import polars as pl
text = "a brown fox jumps over a lazy dog's head"
step = 3
df = pl.DataFrame({"a":text.split(" ")})


first =  df.filter(pl.int_range(pl.len())%step==0)
second = df.filter(pl.int_range(pl.len())%step==1)
third=   df.filter(pl.int_range(pl.len())%step==2)

dff = (
    pl.DataFrame({
        'first':first['a'], 
        'second':second['a'], 
        'third':third['a']})
)
print(dff)

shape: (3, 3)
┌───────┬────────┬───────┐
│ first ┆ second ┆ third │
│ ---   ┆ ---    ┆ ---   │
│ str   ┆ str    ┆ str   │
╞═══════╪════════╪═══════╡
│ a     ┆ brown  ┆ fox   │
│ jumps ┆ over   ┆ a     │
│ lazy  ┆ dog's  ┆ head  │
└───────┴────────┴───────┘
# 

I have the impression that this should be easily solved in a single chain of expressions but I haven't managed to do so. Any suggestions?


Solution

  • A dedicated DataFrame.unstack() has since been added to Polars for this task.

    import polars as pl
    text = "a brown fox jumps over a lazy dog's head"
    step = 3
    df = pl.DataFrame({"a":text.split(" ")})
    
    df.unstack(3, how="horizontal")
    
    shape: (3, 3)
    ┌───────┬───────┬──────┐
    │ a_0   ┆ a_1   ┆ a_2  │
    │ ---   ┆ ---   ┆ ---  │
    │ str   ┆ str   ┆ str  │
    ╞═══════╪═══════╪══════╡
    │ a     ┆ brown ┆ fox  │
    │ jumps ┆ over  ┆ a    │
    │ lazy  ┆ dog's ┆ head │
    └───────┴───────┴──────┘