1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

great! Thanks again.
@GeorgHeiler, sure! It was a perfect question - very clear and fully reproducible (including python code) - good job! :)

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.