pythondataframepython-polars

How to combine columns in polars horizontally?


I'm trying to combine multiple columns into 1 column with python Polars. However I don't seem to find an (elegant) way to combine columns into a list.

I only need to combine column b - e into 1 column. Column a needs to stay exactly as it is now. I've tried using map_elements to achieve this. Despite the fact that this isn't working its also slow and must likely not the best way to do this.

Anyone who can help me out with how I can achieve this result?

The dataframe I have:

df = pl.from_repr("""
┌─────┬─────┬─────┬─────┬─────┐
│ a   ┆ b   ┆ c   ┆ d   ┆ e   │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════╪═════╪═════╪═════╪═════╡
│ 0.1 ┆ 1.1 ┆ 2.1 ┆ 3.1 ┆ 4.1 │
│ 0.2 ┆ 1.2 ┆ 2.2 ┆ 3.2 ┆ 4.2 │
│ 0.3 ┆ 1.3 ┆ 2.3 ┆ 3.3 ┆ 4.3 │
└─────┴─────┴─────┴─────┴─────┘
""")

The result I need:

shape: (3, 2)
┌─────┬──────────────────────┐
│ a   ┆ value                │
│ --- ┆ ---                  │
│ f64 ┆ list[f64]            │
╞═════╪══════════════════════╡
│ 0.1 ┆ [1.1, 2.1, 3.1, 4.1] │
│ 0.2 ┆ [1.2, 2.2, 3.2, 4.2] │
│ 0.3 ┆ [1.3, 2.3, 3.3, 4.3] │
└─────┴──────────────────────┘

Solution

  • Use concat_list.

    pl.Config(fmt_table_cell_list_len=10, fmt_str_lengths=80) # increase repr defaults
    
    df.select('a', value=pl.concat_list(pl.exclude('a')))
    
    shape: (3, 2)
    ┌─────┬──────────────────────┐
    │ a   ┆ value                │
    │ --- ┆ ---                  │
    │ f64 ┆ list[f64]            │
    ╞═════╪══════════════════════╡
    │ 0.1 ┆ [1.1, 2.1, 3.1, 4.1] │
    │ 0.2 ┆ [1.2, 2.2, 3.2, 4.2] │
    │ 0.3 ┆ [1.3, 2.3, 3.3, 4.3] │
    └─────┴──────────────────────┘