[Python] pandas - select row of maximum value_counts

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have this simple dataframe df:

    User,C,G
    111,ar,1
    112,es,1
    112,es,1
    112,es,2
    113,ca,2
    113,ca,3
    113,ca,3
    114,en,4


    I grouped that by:

    result = df.groupby(['User','G'])['C'].value_counts()


    obtaining:

    User G
    111 1 ar 1
    112 1 es 2
    2 es 1
    113 2 ca 1
    3 ca 2
    114 4 en 1


    My goal is then to keep only the rows with the maximum value_count per group, so that the resulting dataframe appears:

    User G
    111 1 ar 1
    112 1 es 2
    113 3 ca 2
    114 4 en 1


    I found also this question related to a similar issues, but I can't figured out how to apply that method in my case.

    Continue reading...

Compartilhe esta Página