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,292
  • 2
  • 19
  • 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
  • 12,057
  • 5
  • 39
  • 55
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
  • 47,092
  • 13
  • 101
  • 128