[Python] sort pandas dataframe by sum of columns

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that looks like this

    Australia Austria United Kingdom Vietnam
    date
    2020-01-30 9 0 1 2
    2020-01-31 9 9 4 2


    I would like to crate a new dataframe that inclues countries that have sum of their column > 4 and I do it

    df1 = df[[i for i in df.columns if int(df.sum()) > 4]]


    this gives me

    Australia Austria United Kingdom
    date
    2020-01-30 9 0 1
    2020-01-31 9 9 4


    I now would like to sort the countries based on the sum of their column and than take the first 2

    Australia Austria
    date
    2020-01-30 9 0
    2020-01-31 9 9


    I know I have to use sort_values and tail. I just can't workout how

    Continue reading...

Compartilhe esta Página