Dario
Dario

Reputation: 321

Pandas: sort according to a row

I have a Dataframe like this (with labels on rows and columns):

    0 1 2 3

0   1 1 0 0
1   0 1 1 0
2   1 0 1 0
-1  5 6 3 2

I would like to order the columns according to the last row (and then drop the row):

    0 1 2 3

0   1 1 0 0
1   1 0 1 0
2   0 1 1 0

Upvotes: 0

Views: 43

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try np.argsort to get the order, then iloc to rearrange columns and drop rows:

df.iloc[:-1, np.argsort(-df.iloc[-1])]

Output:

   1  0  2  3
0  1  1  0  0
1  1  0  1  0
2  0  1  1  0

Upvotes: 1

Related Questions