Georg Heiler
Georg Heiler

Reputation: 17676

pandas multi index sort directly

I asked this question: pandas multi index sort specific fields as a follow up I would like to improve a little bit and perform the sorting directly with multi indices:

Here is a sample df

df = pd.DataFrame({'modelName':['model1','model1', 'model2', 'model2'],
                           'scoringValue':[7,8,9,7]})

Which results in the following overview

overview = df.groupby([df.modelName]).describe().unstack(fill_value=0).loc[:, pd.IndexSlice[:, ['mean','std']]]
print(overview)

          scoringValue          
                  mean       std
modelName                       
model1             7.5  0.707107
model2             8.0  1.414214

I want to sort the models by mean of scoringValue, but retain the grouped relationship to std

This could be achieved by

overview.columns = ['{0[0]}_{0[1]}'.format(tup) for tup in overview.columns]
overview.sort_values('scoringValue_mean', ascending=False)

But I rather would like to work directly with the Multi-index (nicer visual representation) and get a result like this one:

          scoringValue          
                  mean       std
modelName                       
model2             8.0  1.414214 
model1             7.5  0.707107

Upvotes: 1

Views: 118

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

What about using DataFrame.sort_index(level=1)?

In [77]: overview.sort_index(level=1, ascending=0)
Out[77]:
          scoringValue
                  mean       std
modelName
model2             8.0  1.414214
model1             7.5  0.707107

Upvotes: 1

Related Questions