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:
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).
>Solution :
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

