Search code examples
pythongroup-bypython-polars

Aggregate in Polars by appending lists


In Python Polars, how can I aggregate by concatenating lists, rather than creating a nested list? For example, I'd like to aggregate this dataframe on id

import polars as pl

df = pl.DataFrame({
    'id': [1, 1],
    'name': [["Bob"], ["Mary", "Sue"]],
})
id name
1 ["Bob"]
1 ["Mary", "Sue"]

and get this result

id name
1 ["Bob", "Mary", "Sue"]

If I use df.group_by('id').agg("name"), I get a nested list, which I don't want:

id name
1 [["Bob"], ["Mary", "Sue"]]

Solution

  • Try using explode on your name column.

    result_df = df.group_by('id').agg(pl.col('name').explode())