[Python] Explode out polars column of list items horizontally to new columns

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a polars native way to explode out a column of lists horizontally?

    e.g. go from this:

    df = pl.DataFrame(
    {
    "letters": ["a", "a", "b", "c"],
    "numbers": [[1, 1], [2, 3], [4, 5], [6, 7]],
    }
    )


    To this?

    shape: (4, 3)
    ┌─────────┬───────────┬───────────┐
    │ letters ┆ numbers_a ┆ numbers_b │
    │ --- ┆ --- ┆ --- │
    │ str ┆ i64 ┆ i64 │
    ╞═════════╪═══════════╪═══════════╡
    │ a ┆ 1 ┆ 1 │
    │ a ┆ 2 ┆ 3 │
    │ b ┆ 4 ┆ 5 │
    │ c ┆ 6 ┆ 7 │
    └─────────┴───────────┴───────────┘


    I am aware of the .explode() method but afaik this is only possible to use vertically

    Continue reading...

Compartilhe esta Página