銀髮族喜歡什麼?
運用目前學到的技巧來建立摘要表,對該表的特定子集進行篩選、排序,並印出最重要的結果。我們要用這些步驟回答「銀髮族最喜歡哪些電影?」這個問題。
注意:在本題中,我們會直接在 .groupby() 方法中以清單指定分組欄位;和影片中的練習不同,影片是先把清單指定給一個變數。
本練習屬於課程
給試算表使用者的 Python
練習說明
- 以
movie_title和ticket_type分組,建立movies_by_ticket_type。 - 篩選
ticket_type為senior,建立senior_ticket_movies。 - 依
ticket_quantity由大到小排序,建立ordered_senior_movies。 - 在主控台列印
ordered_senior_movies的前 3 列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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
____