Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Grouping
13. Sorting values in groups
Summary

Instruction

Good. You probably noticed that the mean values of grouped rows were not sorted. Luckily, we can change that. We'll use the sort_values() function that you already know.

grouped['aces'].mean().sort_values(ascending=False)

We will get the following result:

height
203 5837.000000
185 5060.000000
198 3658.666667
191 2366.500000
180 1689.000000
Name: aces, dtype: float64

Note that we can specify ascending/descending order inside sort_values(), but we don't specify any column names. The expression grouped['aces'].mean() creates a Series, so there is only one column (aces).

Exercise

Group all movies by director (movies_by_director), show the average rating for each director, and sort the results from best to worst ratings.

Stuck? Here's a hint!

Start by grouping the rows by directors:

movies.groupby('director')

Then, select the rating column, and use the mean() function, as in the instruction. Next, use the sort_values function.