How do I sort a dataframe by distance from zero value

Question:

I’ll start by saying I am a Python beginner, I did try and find an answer to this via similar questions but I’m struggling to grasp some of the solutions in order to tailor them for my own use.

If I have a Pandas dataframe as follows:

enter image description here

What code would I need in order to sort it as per the below whilst excluding the 0 value.
I would ideally want to grab the value closest to zero (assuming this is possible).

enter image description here

Asked By: Matt

||

Answers:

IIUC, you can set abs as a key parameter of pandas.DataFrame.sort_values.

Try this :

out = df.sort_values(by="Score", key=abs)

# Output :

print(out)

     Name  Score
3  maggie      0
2   sally     -5
1    jane    -10
4   peter     15
6    andy     25
0     bob    -30
5    mike     50
Answered By: abokey

You can use df.sort_values() and pass abs as a parameter. This will sort by the absolute value but leave the values themselves unchanged:

import pandas as pd

df = pd.DataFrame({'Name': ['Bob', 'Jane', 'Sally', 'Maggie', 'Peter', 'Mike', 'Andy'],
                    'Score': [-30, -10, -5, 0, 15, 50, 25]})

df.sort_values('Score', key = abs)

Output:

Name Score
Maggie 0
Sally -5
Jane -10
Peter 15
Andy 25
Bob -30
Mike 50

This also works:

df.reindex(df['Score'].abs().sort_values().index)

See here for more:
Sorting by absolute value without changing the data

Answered By: Marty_C137
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.