2017-04-20 54 views
2

排序的列我得到了以下數據框:在數據幀

r1  Angle1   r2  Angle  Res   System 
0 1.250590 5.156790 5.724081 7.712978e+06 8.874775  #EN1 
1 1.246085 5.678428 5.720825 3.353667e+04 8.813887  #EN2 
2 1.250148 5.205040 5.700498 3.564115e+02 8.865881  #EN1 
3 1.241692 6.256054 5.708635 8.518839e+06 8.828826  #EN3 
4 1.247089 5.556371 5.718326 9.315014e+04 8.813084  #EN1 
5 1.854719 0.936551 1.186143 1.853106e+01 8.669692  #EN2 
6 1.260329 4.225127 5.687622 5.435705e+01 9.223529  #EN3 
7 1.251378 5.072078 5.756885 1.449325e+01 8.893499  #EN2 
8 1.037451 39.403842 1.340242 2.438089e+04 14.509603  #EN1 

我想根據過去的系統對它進行排序。我試圖完成它:

custom_dict = {'#EN1':0, '#EN2':1, '#EN3':2} 
    s = df_ALL['System'].apply(lambda x: custom_dict[x]) 
    s.sort() 
    df_ALL.set_index(s.index).sort() 

但是我得到相同的DataFrame作爲回報。在它之上彈出警告:

FutureWarning: sort is deprecated, use sort_values(inplace=True) for INPLACE sorting 
    s.sort() 
FutureWarning: sort(....) is deprecated, use sort_index(.....) 
    df_ALL = df_ALL.set_index(s.index).sort() 

有人可以詳細說明我在做什麼錯嗎?

+1

的消息告訴你使用'sort_values'。你試過了嗎。 。 。使用'sort_values'? – BrenBarn

回答

2

看來你需要sort_values

df = df.sort_values('System') 
print (df) 
     r1  Angle1  r2   Angle  Res System 
0 1.250590 5.156790 5.724081 7.712978e+06 8.874775 #EN1 
2 1.250148 5.205040 5.700498 3.564115e+02 8.865881 #EN1 
4 1.247089 5.556371 5.718326 9.315014e+04 8.813084 #EN1 
8 1.037451 39.403842 1.340242 2.438089e+04 14.509603 #EN1 
1 1.246085 5.678428 5.720825 3.353667e+04 8.813887 #EN2 
5 1.854719 0.936551 1.186143 1.853106e+01 8.669692 #EN2 
7 1.251378 5.072078 5.756885 1.449325e+01 8.893499 #EN2 
3 1.241692 6.256054 5.708635 8.518839e+06 8.828826 #EN3 
6 1.260329 4.225127 5.687622 5.435705e+01 9.223529 #EN3 

但如果需要自定義排序dict使用ordered categorical

custom_dict = {'#EN1':0, '#EN2':2,'#EN3':1} 
#get order by sorted values of dict 
cat = sorted(custom_dict, key=custom_dict.get) 
print (cat) 
['#EN1', '#EN3', '#EN2'] 

df['System'] = df['System'].astype('category', categories=cat, ordered=True) 
df = df.sort_values('System') 
print (df) 
     r1  Angle1  r2   Angle  Res System 
0 1.250590 5.156790 5.724081 7.712978e+06 8.874775 #EN1 
2 1.250148 5.205040 5.700498 3.564115e+02 8.865881 #EN1 
4 1.247089 5.556371 5.718326 9.315014e+04 8.813084 #EN1 
8 1.037451 39.403842 1.340242 2.438089e+04 14.509603 #EN1 
3 1.241692 6.256054 5.708635 8.518839e+06 8.828826 #EN3 
6 1.260329 4.225127 5.687622 5.435705e+01 9.223529 #EN3 
1 1.246085 5.678428 5.720825 3.353667e+04 8.813887 #EN2 
5 1.854719 0.936551 1.186143 1.853106e+01 8.669692 #EN2 
7 1.251378 5.072078 5.756885 1.449325e+01 8.893499 #EN2