Search code examples
pythonlistpython-polars

Python-Polars merge lists of lists


Hello I have a polars dataframe with a column that contains lists of lists. like this:

df = pl.DataFrame({
    "nodes": [[[1], [1,2], []], [[2]], [[1], [2]]]
})

I want to merge all these sublists into one single list like this:

shape: (3, 1)
┌───────────┐
│ nodes     │
│ ---       │
│ list[i64] │
╞═══════════╡
│ [1, 1, 2] │
│ [2]       │
│ [1, 2]    │
└───────────┘

I am pretty sure that must be doable pretty easily but I don't find the correct way in the API. Thanks for any help!


Solution

  • df.select(
        pl.col.nodes.list.eval(pl.element().explode().drop_nulls())
    )
    
    shape: (3, 1)
    ┌───────────┐
    │ nodes     │
    │ ---       │
    │ list[i64] │
    ╞═══════════╡
    │ [1, 1, 2] │
    │ [2]       │
    │ [1, 2]    │
    └───────────┘