I'm trying to sort data in a multilevel index for visualisation. At this point it is purely to order the data based on the values
I've tried working with sort_index and sort_values, however neither have worked. I'm assuming there is a way to combine the 2 that is not clear to me.
Example code
import pandas as pd
data = {'lev1':[1,1,2,2],
'lev2':['item1', 'item2', 'item3', 'item2'],
'col1':[.55, .44, .22, .34],
'col2':[.54, .86, .55, .44]}
df = pd.DataFrame(data=data)
df.set_index(['lev1', 'lev2'], inplace=True)
This should result in :
col1 col2
lev1 lev2
1 item1 0.55 0.54
item2 0.44 0.86
2 item3 0.22 0.55
item2 0.34 0.44
What I would like to see is the output ordered based on the values in col2. However, keeping the multilevel index intact.
Meaning, the results should show:
col1 col2
lev1 lev2
1 item2 0.44 0.86
item1 0.55 0.54
2 item3 0.22 0.55
item2 0.34 0.44
Any ideas or suggestions are welcome.
Thank you!