S. Quon
S. Quon

Reputation: 47

Reordering rows based on number formed by combining columns in Python

I have a dataframe formed with pandas as seen below:

        a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
    1   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
    2   0  0  0  0  0  0  1  0  1  0  1  0  0  1  0
    3   0  1  0  0  1  0  1  0  1  0  1  0  0  1  0
    4   0  0  0  0  0  1  0  1  0  0  1  0  0  1  0
    5   0  0  1  1  0  1  0  1  0  0  1  0  0  1  0
    6   1  0  0  1  0  1  0  1  0  0  1  0  0  1  0
    7   0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
    8   0  0  0  0  0  0  0  0  1  0  1  0  0  1  0
    9   0  0  0  0  0  0  0  0  0  0  0  1  1  1  0
    10  0  0  0  1  0  1  0  1  0  0  1  0  0  1  0
    11  0  0  0  0  0  0  0  1  0  0  1  0  0  1  0
    12  0  0  0  0  1  0  1  0  1  0  1  0  0  1  0
    13  0  0  0  0  0  0  0  0  0  0  1  0  0  1  0
    14  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
    15  0  0  0  0  1  0  1  0  1  0  1  0  0  1  0
    16  0  0  0  0  0  0  0  0  0  0  0  1  0  1  0

I want to sort the rows so that they are ordered in descending order. The value of the row is the number formed by combining the columns. For example, row 1 is 000000000000000 and row 2 is 000000101010010. The final result should have Row 6 as the first row and row 1 as the last row. I've tried

    dat.sort_values(by=['a'], ascending=False, axis=0)

but this only sorts by the first column. Is there another way I could reorder the rows?

Upvotes: 0

Views: 48

Answers (2)

BENY
BENY

Reputation: 323316

Using a key for sort

df.loc[df.astype(str).sum(1).sort_values(ascending=False).index]
Out[871]: 
    a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
6   1  0  0  1  0  1  0  1  0  0  1  0  0  1  0
3   0  1  0  0  1  0  1  0  1  0  1  0  0  1  0
5   0  0  1  1  0  1  0  1  0  0  1  0  0  1  0
10  0  0  0  1  0  1  0  1  0  0  1  0  0  1  0
15  0  0  0  0  1  0  1  0  1  0  1  0  0  1  0
12  0  0  0  0  1  0  1  0  1  0  1  0  0  1  0
4   0  0  0  0  0  1  0  1  0  0  1  0  0  1  0
2   0  0  0  0  0  0  1  0  1  0  1  0  0  1  0
11  0  0  0  0  0  0  0  1  0  0  1  0  0  1  0
8   0  0  0  0  0  0  0  0  1  0  1  0  0  1  0
13  0  0  0  0  0  0  0  0  0  0  1  0  0  1  0
9   0  0  0  0  0  0  0  0  0  0  0  1  1  1  0
16  0  0  0  0  0  0  0  0  0  0  0  1  0  1  0
7   0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
14  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
1   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

Upvotes: 2

akuiper
akuiper

Reputation: 215037

Sort by all columns in their current order:

df.sort_values(by=df.columns.tolist(), ascending=False)

#    a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
#6   1  0  0  1  0  1  0  1  0  0  1  0  0  1  0
#3   0  1  0  0  1  0  1  0  1  0  1  0  0  1  0
#5   0  0  1  1  0  1  0  1  0  0  1  0  0  1  0
#10  0  0  0  1  0  1  0  1  0  0  1  0  0  1  0
#12  0  0  0  0  1  0  1  0  1  0  1  0  0  1  0
#15  0  0  0  0  1  0  1  0  1  0  1  0  0  1  0
#4   0  0  0  0  0  1  0  1  0  0  1  0  0  1  0
#2   0  0  0  0  0  0  1  0  1  0  1  0  0  1  0
#11  0  0  0  0  0  0  0  1  0  0  1  0  0  1  0
#8   0  0  0  0  0  0  0  0  1  0  1  0  0  1  0
#13  0  0  0  0  0  0  0  0  0  0  1  0  0  1  0
#9   0  0  0  0  0  0  0  0  0  0  0  1  1  1  0
#16  0  0  0  0  0  0  0  0  0  0  0  1  0  1  0
#7   0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
#14  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
#1   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

Upvotes: 3

Related Questions