본문 바로가기

Algorithm/Python

[stratascratch] Largest Olympics(Python)

문제

Find the Olympics with the highest number of athletes. The Olympics game is a combination of the year and the season, and is found in the 'games' column. Output the Olympics along with the corresponding number of athletes.

선수 수가 가장 많은 올림픽 찾기. 해당 선수 수와 함께 games를 출력

코드

# Import your libraries
import pandas as pd

# Start writing code
olympics_athletes_events.head()
olympics_athletes_events[['games','id']].drop_duplicates().groupby("games")["id"].count().reset_index().sort_values(by="id", ascending=False).head(1)

SQL에서 distinct는 pandas에서는 nunique()라는 집계함수를 사용할 수 있음

cnt = olympics_athletes_events.groupby("games")["id"].nunique()
cnt = cnt.count().reset_index().sort_values(by="id", ascending=False).head(1)

출처

 

반응형