我得到了以下DataFrame:
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()
愿任何人详细说明我做错了吗?
答案 0 :(得分: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