How to sort a dataframe by String Index and Intiger Column together using Custom list and Descending order

Viewed 13

I have a data frame like this:

l1 = ['Prime','Prime','Trim','Mesh','Leather','Cemat','Cemat']
l2 = ['M','B','A','F','G','E','C']
v1 = [20,44,11,34,46,60,55]

d = {'Type': l1, 'Model': l2,'Sum': v1} 
df = pd.DataFrame(d)
df = df.set_index(['Type','Model'])

I want to sort this data frame by level-0 Index in custom order & Sum column in descending order.

For sorting Level_0[Type] Index, I want to sort in this order: [Trim, Mesh, Leather, Prime, Cemat]

Sometimes I might not have all the 5 values in the index but the sorting order is still the same

I tried this but could not get the ideal result as it is not sorting index my required order:

df.sort_values(by = ['Type','Sum'], ascending = [False, False])

Can anyone help me with this?

0 Answers
Related