[Python] How to group dataframe rows into list in polars group_by

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    import polars as pl

    df = pl.DataFrame(
    {
    'Letter': ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'D','D','E'],
    'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9,10]
    }
    )


    I want to group Letter and collect their corresponding Value in a List.

    Related Pandas question: How to group dataframe rows into list in pandas groupby

    I know pandas code will not work here:

    df.group_by('a')['b'].apply(list)

    TypeError: 'GroupBy' object is not subscriptable


    Output will be:

    | A ┆ [1, 2] │
    │ B ┆ [3, 4, 5] │
    │ C ┆ [6, 7] │
    │ D ┆ [8, 9] │
    │ E ┆ [10] |

    Continue reading...

Compartilhe esta Página