[Python] Select rows with highest value from groupby [duplicate]

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that contains some information about users. There is a column for user, column for type, and column for count, like this:

    name type count
    robert x 123
    robert y 456
    robert z 5123
    charlie x 442123
    charlie y 0
    charlie z 42


    I'm trying to figure out which type has the highest count per name, so for this case, I would want to select this:

    name type count
    robert z 5123
    charlie x 442123


    I know I can do something like this to get the max count per name, but I'm not sure how I can include the "type" column, which is actually the most important

    df.sort_values('count', ascending=False).drop_duplicates('name').sort_index()

    Continue reading...

Compartilhe esta Página