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!
pl.Expr.list.eval()
and pl.element()
to run Expr
evaluation within outer list.pl.Expr.explode()
to explode inner lists.pl.Expr.drop_nulls()
to drop result from exploding empty list.df.select(
pl.col.nodes.list.eval(pl.element().explode().drop_nulls())
)
shape: (3, 1)
┌───────────┐
│ nodes │
│ --- │
│ list[i64] │
╞═══════════╡
│ [1, 1, 2] │
│ [2] │
│ [1, 2] │
└───────────┘