如何按行名称对数据框进行排序?

Raj*_*ngh 1 python pandas

我有一个数据框

  a b c d
b 0 0 0 1
d 1 0 0 1
c 1 0 0 0
a 2 0 0 1
Run Code Online (Sandbox Code Playgroud)

如何按行名称对 df 进行排序,例如

  a b c d
a 2 0 0 1
b 0 0 0 1
c 1 0 0 0
d 1 0 0 1
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 5

您可以使用sort_index

df.sort_index(inplace=True) 
Run Code Online (Sandbox Code Playgroud)