2016-07-27 138 views
2

我有一個多索引熊貓數據框需要按索引器之一排序。這裏是一段數據:將一個熊貓數據框排序一個多層索引

gene      VIM 
treatment dose time    
TGFb  0.1 2 -0.158406 
      1 2  0.039158 
      10 2 -0.052608 
      0.1 24 0.157153 
      1 24 0.206030 
      10 24 0.132580 
      0.1 48 -0.144209 
      1 48 -0.093910 
      10 48 -0.166819 
      0.1 6  0.097548 
      1 6  0.026664 
      10 6 -0.008032 

我正在尋找排序的數據,使時間索引是升序。我的第一個想法是使用pandas.sort_values,但它似乎不適用於索引。有沒有人知道一種方法來做到這一點?由於

回答

3

使用sort_index指定level

df.sort_index(level=2) 

或者

df.sort_index(level=-1) 

或者

df.sort_index(level='time') 

全部產量:

enter image description here

相關問題