Custom Sorting Using Pandas

I have the following framework, which I would like to sort first by Criticality, and then by name:

Name Criticality baz High foo Critical baz Low foo Medium bar High bar Low bar Medium ... 

I tried to do this using the answer provided in this post , but I just can't get it to work.

The end result should be like this:

 Name Criticality bar High bar Medium bar Low baz High baz Low foo Critical foo Medium 
+7
python sorting pandas
source share
1 answer

One approach is to use a custom dict to create a β€œrank” column, which is then used to sort and then delete the column after sorting:

 In [17]: custom_dict = {'Critical':0, 'High':1, 'Medium':2, 'Low':3} df['rank'] = df['Criticality'].map(custom_dict) df Out[17]: Name Criticality rank 0 baz High 1 1 foo Critical 0 2 baz Low 3 3 foo Medium 2 4 bar High 1 5 bar Low 3 6 bar Medium 2 [7 rows x 3 columns] In [19]: # now sort by 'Name' and 'rank', it will first sort by 'Name' column first and then 'rank' df.sort(columns=['Name', 'rank'],inplace=True) df Out[19]: Name Criticality rank 4 bar High 1 6 bar Medium 2 5 bar Low 3 0 baz High 1 2 baz Low 3 1 foo Critical 0 3 foo Medium 2 [7 rows x 3 columns] In [21]: # now drop the 'rank' column df.drop(labels=['rank'],axis=1) Out[21]: Name Criticality 4 bar High 6 bar Medium 5 bar Low 0 baz High 2 baz Low 1 foo Critical 3 foo Medium [7 rows x 2 columns] 
+18
source share

All Articles