2

I got following 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

I want to sort it based on the last System. I have tried to accomplish it:

    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()

However I'm getting the same DataFrame in return. On top of it the warning pops up:

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() 

May anybody elaborate what I'm doing wrong, please?

1
  • 1
    The message is telling you to use sort_values. Did you try. . . using sort_values? Commented Apr 20, 2017 at 5:37

1 Answer 1

2

It seems you need 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

But if need custom sort by dict use 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.