再来整合一次
现在我们更清楚地了解合并如何丰富数据了,让我们回到一个汇总表。
当前已加载 2 个 DataFrame:
transactions—— 每笔电影票销售交易的完整列表,但不包含电影类型信息。movies—— 电影片名与类型的表。
让我们把这两张表合并起来,重新得到之前习以为常的视图——每种类型售出的票数。
本练习是课程的一部分
面向表格用户的 Python
练习说明
- 在
movie_title列上将transactions与movies合并。 - 按
movie_genre分组并求和。将结果保存到genre_summary。 - 按
ticket_quantity对genre_summary排序。将结果保存为genre_summary_sorted。 - 打印
genre_summary_sorted(此步骤已为您完成)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Merge transaction data with the movie data on movie_title
transactions_with_genres = ____
# Group by movie_genre and call the sum method
genre_summary = transactions_with_genres.groupby(____, as_index=False).____()
# Sort the genre summary by ticket_quantity
genre_summary_sorted = genre_summary.____('ticket_quantity', ascending=False).reset_index(drop=True)
# View the summary
print(genre_summary_sorted)