[Python] How do I flatten the elements of a column of type list of lists so that it is a...

Discussão em 'Python' iniciado por Stack, Outubro 31, 2024.

  1. Stack

    Stack Membro Participativo

    Consider the following example:

    import polars as pl

    pl.DataFrame(pl.Series("x", ["1, 0", "2,3", "5 4"])).with_columns(
    pl.col("x").str.split(",").list.eval(pl.element().str.split(" "))
    )


    shape: (3, 1)
    ┌────────────────────┐
    │ x │
    │ --- │
    │ list[list[str]] │
    ╞════════════════════╡
    │ [["1"], ["", "0"]] │
    │ [["2"], ["3"]] │
    │ [["5", "4"]] │
    └────────────────────┘


    I want to flatten the elements of the column, so instead of being a nested list, the elements are just a list. How do I do that?

    Continue reading...

Compartilhe esta Página