1

I have a dataframe

  a b c d
b 0 0 0 1
d 1 0 0 1
c 1 0 0 0
a 2 0 0 1

How to sort the df by row names like

  a b c d
a 2 0 0 1
b 0 0 0 1
c 1 0 0 0
d 1 0 0 1
Rajeev Singh
  • 3,252
  • 2
  • 18
  • 30
  • The only link you need to make for the marked duplicate is your "row names" are, in fact, the dataframe's `index`. Indexing has a special purpose in Pandas, you can read up more in the official docs. – jpp Jun 13 '18 at 08:19

2 Answers2

5

You can use sort_index:

df.sort_index(inplace=True) 
Joe
  • 11,147
  • 5
  • 36
  • 50
2

Just use sort_index method. Also, you have to pass inplace=True property in order to operate in place.

df.sort_index(inplace=True)
Mihai Alexandru-Ionut
  • 44,345
  • 11
  • 88
  • 115