Sort values in a Pandas DataFrame along all axis and get their index

There are no doubts about the power of Pandas and how useful it is in Data Science and Data analysis.

· 1 min read
Sort values in a Pandas DataFrame along all axis and get their index
“Close-up of lines of code on a computer screen” by Ilya Pavlov on Unsplash

There are no doubts about the power of Pandas and how useful it is in Data Science and Data analysis.

I’m using it extensively in my current internship project along with other libraries in this stack such as Scikit-learn and Numpy. I encountered lots of unique cases in the paper I’m implementing and decided to share solutions and hacks for those cases so that everyone can utilize them and save their time. For today’s topic, I will also explore the opportunities of submitting a pull request to be added as a feature.

For sorting all the values in the data frame, unlike .sort_values() method which requires a by= argument we will first unstack our data frame and sort values in the order we want (descending in this example). Moreover, we can get the indices of their appearance using .index.values method.

For Jupyter notebook version of this code snippet, take a look at the following repository.

HamedMP/CodeSnippets
CodeSnippets — List of code snippets I found useful and usually arrived to them after spending around 5 to 50 Google…