Best-selling movie by location
With our new tools, we can create even more useful tables. Recall earlier, when we found the best-selling movie in Portland - we had to filter our table for Portland films only, then sort the values to find our best-seller. It would be pretty time consuming to do this for every theater location.
Instead, using some of our new tools, we can quickly condense our sales
transaction data down to a summary table, sort that summary table, and get the top movie for each market all at once.
This exercise is part of the course
Python for Spreadsheet Users
Exercise instructions
- Use
.groupby()
and.sum()
to create a summary table bytheater_location
andmovie_title
. - Sort
totals
byticket_quantity
in descending order. - Use
.groupby()
and.head()
to extract the top movie bytheater_location
. - Print
top_movies
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a summary by theater location and movie title
totals = sales.____([____, ____], as_index=False).____()
# Sort totals by ticket quantity in descending order
totals_sorted = totals.____(____, ascending=False).reset_index(drop=True)
# Take the top row for each theater location
top_movies = totals_sorted.____(____).____(1).reset_index(drop=True)
# Print results
____