[Python] how to find the top N or bottom N numeric values of a DF after a group by [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    This prints out the 10 worst mean values in a dataframe (this dataframe was constructed with an agg, so it has a multi-level column name).

    worst = rcresult.sort_values(by=('resolvetime', 'mean'), ascending=False).head(10)


    The rcresult dataframe also contains a string field called ym (a combination of year and month, it reads like: 2025 05) .

    As a side note re: this agg df, if I print out a column list the ym column shows up as ('ym', ''), but in Pandas I can access it like so:

    print(rcresult['ym'].head(5))


    What is the correct syntax to get the 10 worst mean values per 'ym' block?

    Continue reading...

Compartilhe esta Página