0

Dataframes looks something like

Names            Rank
Michael            8
David              6
Christopher        6
Brian              5
Amanda             3
Heather            8
Sarah              2
Rebecca            4

Expected O/P

Names         Rank
Heather        8
Michael        8
Christopher    6
David          6
Brian          5
Rebecca        4
Amanda         3
Sarah          2

Here, I need to first sort the rank column in descending order and then the Name column in alphabetical order.

My code :

df = df.sort_values(['Name'],ascending = True)
df = df.'Name'.sort_values(['Rank'],ascending = False)
df

This code gives me on the sorted rank but the Name column doesn't get sorted.

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Darakhsha
  • 1
  • 1
  • Does this answer your question? [How to sort a dataframe by multiple column(s)](https://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-multiple-columns) – ababuji May 17 '20 at 12:20

1 Answers1

0

df = df.sort_values(['Rank', 'Name'],ascending = [False, True])

Darakhsha
  • 1
  • 1