The .sort_values() method
Let's say you wanted to look at the top 10 theater/movie combinations in your data. Maybe you want to build another theater in your best-performing city, or see if a smash hit movie was generating impressive ticket sales in all of your theater locations. A start to this process might be to sort your data by ticket sales and look at the top few rows.
That's one place where sort_values()
comes in handy. Let's use it to answer the question what are our top 3 movie/theater combinations?
This exercise is part of the course
Python for Spreadsheet Users
Exercise instructions
- Use the
.sort_values()
method to sort sales byticket_quantity
, and set theascending
argument in.sort_values()
toFalse
so the highest-selling movies are first. - Display the top 3 best-selling movies using the
.head()
method.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the pandas package as pd
import pandas as pd
# Read in the Excel file
sales = pd.read_excel('ticket_sales.xlsx')
# Sort sales by ticket quantity descending
sales_sorted = sales.____(____, ascending=____)
sales_sorted = sales_sorted.reset_index(drop=True)
# Print top 3 rows of sorted data
print(sales_sorted.____(____))