Frederic
Frederic

Reputation: 281

Pandas - Sort dataframe by highest individual value in any column

I have data formatted like this

df = pd.DataFrame(data=[['Steve', '3', '2'],
                        ['Smith', '4', '3'],
                        ['Scott', '0', '5'],
                        ['Simon', '1', '8']],
                  columns=['Name', 'Count1', 'Count2'])

Sorting it with df.sort_values(by=['Count1','Count2'], ascending=[False,False], inplace=True) returns this

    Name Count1 Count2
1  Smith      4      3
0  Steve      3      2
3  Simon      1      8
2  Scott      0      5

I want to get the rows with the highest individual value of any column to the top. This is my expected output:

    Name Count1 Count2
3  Simon      1      8
2  Scott      0      5
1  Smith      4      3
0  Steve      3      2

What would be the appropriate way of going about this?

Upvotes: 3

Views: 884

Answers (3)

haitham
haitham

Reputation: 3536

Compute the max of the columns, and then sort and reference the index.

df.iloc[df.iloc[:, 1:].max(axis=1).sort_values(ascending=False).index]
    Name Count1 Count2
3  Simon      1      8
2  Scott      0      5
1  Smith      4      3
0  Steve      3      2

Upvotes: 1

Dani Mesejo
Dani Mesejo

Reputation: 61910

You could do:

result = df.assign(order=df.iloc[:, 1:].max(axis=1)).sort_values('order', ascending=False).drop('order', axis=1)
print(result)

Output

    Name Count1 Count2
3  Simon      1      8
2  Scott      0      5
1  Smith      4      3
0  Steve      3      2

As an alternative:

order = df.drop('Name', axis=1).max(1).sort_values(ascending=False).index
result = df.iloc[order]
print(result)

Upvotes: 1

BENY
BENY

Reputation: 323226

IIUC argsort

df = df.iloc[(-df.drop('Name',1).max(axis=1)).argsort()]
    Name Count1 Count2
3  Simon      1      8
2  Scott      0      5
1  Smith      4      3
0  Steve      3      2

Upvotes: 5

Related Questions