MohammedE
MohammedE

Reputation: 109

Ranking each column from higher to lower

I have a dataset which shows the accuracy values obtained by different method across different datasets. Each row represents one dataset. The dataframe looks like this:

Method_1   Method_2   Method_3   Method_4
  84.3       82.1       90.7       73.5
  82.1       79.8       83.3       60.2
  84.2       88.5       90.1       95.3

I need to rank each row (from 1 to 4) so that 1 is the highest or larges value and 4 in the lowest value.

So the output should be like this:

Method_1   Method_2   Method_3   Method_4
   2          3          1          4
   2          3          1          4
   4          3          2          1

Any idea how to implement it?

Upvotes: 0

Views: 120

Answers (1)

mozway
mozway

Reputation: 260725

You can use rank with ascending=False:

df.rank(axis=1, ascending=False).convert_dtypes()

NB. in case of ties, use the method parameter to set up the desired option (min/max/average/first/dense)

output:

   Method_1  Method_2  Method_3  Method_4
0         2         3         1         4
1         2         3         1         4
2         4         3         2         1

Upvotes: 1

Related Questions