지역별 최다 판매 영화
이제 새 도구를 활용해 더 유용한 표를 만들 수 있어요. 앞서 포틀랜드에서 최다 판매 영화를 찾을 때를 떠올려 보세요. 포틀랜드 영화만 필터링한 뒤 값을 정렬해 베스트셀러를 찾았죠. 이 작업을 각 극장 위치마다 반복한다면 시간이 아주 많이 걸릴 거예요.
대신, 새 도구를 이용해 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
____