各地區的最暢銷電影
有了新工具,我們可以建立更實用的表格。回想一下,先前要找出波特蘭的最暢銷電影時——我們必須先將表格篩成只有波特蘭的電影,再排序找出銷售冠軍。若要對每個戲院地點都做一次,會非常耗時。
改用這些新工具,我們可以先把 sales 交易資料彙整成摘要表、對摘要表排序,然後一次就取得各市場的銷售冠軍電影。
本練習屬於課程
給試算表使用者的 Python
練習說明
- 使用
.groupby()搭配.sum(),依theater_location與movie_title建立摘要表。 - 以遞減順序依
ticket_quantity對totals排序。 - 使用
.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
____