Search code examples
pandaspandasql

Is there a pandas function can group data by multiple columns from least to greatest?


I am trying to group rows of data that have close values by column. The goal is to sort rows of data from least to greatest. The first row has the lowest values of all the rows then the 2nd row has the next lowest values. Is pandas the best way to go about? I'll take any advice.

d1  d2  d3  d4   d5  d6
 2    2   2   4   8   10
 5    2   3   5   7    9
 9    1   6   9  11   15
13    4   2   1  21   12
 3    5   7   6  11   11

desired out put:

 d1  d2  d3  d4   d5  d6
 2    2   2   4   8   10
 5    2   3   5   7    9
 3    5   7   6  11   11
 9    1   6   9  11   15
13    4   2  10  21   12

Solution

  • I believe you need to sum all columns using df.sum() on axis=1 and reindex using argsort

    out = df.reindex(df.sum(1).argsort())
    

    print(out)
    
       d1  d2  d3  d4  d5  d6
    0   2   2   2   4   8  10
    1   5   2   3   5   7   9
    4   3   5   7   6  11  11
    2   9   1   6   9  11  15
    3  13   4   2   1  21  12