[Python] In Polars, how do you create a group counter / group ID?

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    How do you get a group_id column like this, grouping by columns col1 and col2 ?

    col1 col2 group_id
    A Z 1
    A Y 2
    A Z 1
    B Z 3

    based on such a DataFrame :

    df = pl.DataFrame({
    'col1': ['A', 'A', 'A', 'B'],
    'col2': ['Z', 'Y', 'Z', 'Z']}
    )


    In other words, I'm looking for polars equivalent to R data.table .GRP (df[, .GRP, by = .(col1, col2)]). Thanks !

    Continue reading...

Compartilhe esta Página