按地点的最畅销电影
有了这些新工具,我们可以创建更实用的表格。回想之前我们在波特兰找出最畅销电影时的做法——需要先把表格筛选为仅包含波特兰的影片,再按数值排序找出销量冠军。若要对每个影院地点都这样做,会非常耗时。
现在,使用这些新工具,您可以先把 sales 交易数据汇总为一张摘要表,对这张摘要表排序,然后一次性得到各个市场的销量冠军影片。
本练习是课程的一部分
面向表格用户的 Python
练习说明
- 使用
.groupby()和.sum(),按theater_location与movie_title创建一张汇总表。 - 将
totals按ticket_quantity降序排序。 - 使用
.groupby()和.head(),按theater_location提取每个地点的销量冠军影片。 - 打印
top_movies。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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
____