Introduction
Grouping
14. Grouping by multiple columns
Summary

Instruction

Perfect! One more thing we can do is group rows by multiple columns. Take a look:

players_by_year_country = players.groupby(['year_born', 'country'])

Instead of providing a single value inside the parentheses, we now provide a list of values. The list is surrounded by square brackets, and each value is separated by a comma. The code above will first group all tennis players by the year they were born in, and then by the country they come from.

You can use statistical functions and sorting with such groups in the same way. For instance:

players_by_year_country = players.groupby(['year_born', 'Country'])
players_by_year_country['aces'].mean().sort_values()

Exercise

Group all movies by the country column, and then by the genre. For each group, find the mean of the box_office column.

Stuck? Here's a hint!

Start with:

movies.groupby(['country', 'genre'])