Sort pandas dataframe both on values of a column and index?

dataframe, pandas, python, sorting

Solution

Pandas 0.23 finally gets you there :-D

You can now pass index names (and not only column names) as parameters to `sort_values`. So, this one-liner works:

df = df.sort_values(by = ['MyCol', 'MyIdx'], ascending = [False, True])

And if your index is currently unnamed:

df.index.name = 'MyIdx'
df = df.sort_values(by = ['MyCol', 'MyIdx'], ascending = [False, True])

Problem

Is it feasible to sort pandas dataframe by values of a column, but also by index? If you sort a pandas dataframe by values of a column, you can get the resultant dataframe sorted by the column, but unfortunately, you see the order of your dataframe's index messy within the same value of a sorted column. So, can I sort a dataframe by a column, such as the column named `count` but also sort it by the value of index? And is it also feasible to sort a column by descending order, but whereas sort a index by ascending order? I know how to sort multiple columns in dataframe, and also know I can achieve what I'm asking here by first `reset_index()` the index and sort it, and then create the index again. But is it more intuitive and efficient way to do it?

Original source