What do seniors like?
Let's use what we've learned so far to create a summary table, sort for a specific subset of that table, sort the values, and print the most important results. We'll use these techniques to answer the question "what movies do seniors like best?"
Note that in this exercise, we'll group by a list directly in the .groupby()
method, as opposed to the video exercise, where we first assigned a list to a variable.
This exercise is part of the course
Python for Spreadsheet Users
Exercise instructions
- Group by
movie_title
andticket_type
to createmovies_by_ticket_type
. - Filter for where
ticket_type
issenior
to createsenior_ticket_movies
. - Sort by
ticket_quantity
in descending order to createordered_senior_movies
. - Print the first 3 rows of
ordered_senior_movies
to the console.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Summarize by movie title and ticket type
movies_by_ticket_type = sales.groupby(____, as_index=False).sum()
# Filter for senior tickets
senior_ticket_movies = movies_by_ticket_type[____[____] == ____].reset_index(drop=True)
# Sort senior ticket sales descending
ordered_senior_movies = senior_ticket_movies.____(____, ascending=____).reset_index(drop=True)
# Print the top 3 rows of the ordered table
____