주어진 전체 기간의 각 나라별 골득점수 상위 5개 국가와
그 득점수를 데이터프레임형태로 출력하라
# s
df.groupby('Country')['Goals'].sum().sort_values(ascending=False).head().to_frame('Goals')
# sort_values(by = ' ')
df.groupby('Country').sum().sort_values('Goals',ascending=False).head()
주어진 전체기간동안 골득점을 한 선수가 가장 많은 나라 상위 5개 국가와 그 선수 숫자를 데이터 프레임 형식으로 출력하라
# s
df.groupby('Country').count().sort_values('Player',ascending=False).head()[['Player']]
# .size()
result = df.groupby('Country').size().sort_values(ascending=False).head(5)
print(result)
Years 컬럼은 년도 -년도 형식으로 구성되어있고, 각 년도는 4자리 숫자이다. 년도 표기가 4자리 숫자로 안된 케이스가 존재한다. 해당 건은 몇건인지 출력하라
df['yearLst'] = df.Years.str.split('-')
def checkFour(x):
for value in x:
if len(str(value)) != 4:
return False
return True
df['check'] = df['yearLst'].apply(checkFour)
result = len(df[df.check ==False])
result
Q3에서 발생한 예외 케이스를 제외한 데이터프레임을 df2라고 정의하고 데이터의 행의 숫자를 출력하라 (아래 문제부터는 df2로 풀이하겠습니다)
# s
len(df[df.check==True])
# .shape[0]
df2 = df[df.check ==True].reset_index(drop=True)
print(df2.shape[0])
월드컵 출전횟수를 나타내는 ‘LenCup’ 컬럼을 추가하고 4회 출전한 선수의 숫자를 구하여라
# s
df2['LenCup'] = [len(i) for i in df2['yearLst']]
len(df2[df2['LenCup'] == 4])
# .str.len()
df2['LenCup'] =df2['yearLst'].str.len()
result = df2['LenCup'].value_counts()[4]
print(result)
Yugoslavia 국가의 월드컵 출전횟수가 2회인 선수들의 숫자를 구하여라
# s
df2[(df2['Country'] =='Yugoslavia') & (df2['LenCup']==2)]['Player'].count()
# skip drop_duplicates('Player')
#
result = len(df2[(df2.LenCup==2) & (df2.Country =='Yugoslavia')])
print(result)
2002년도에 출전한 전체 선수는 몇명인가?
# s
df2[df2.Years.str.contains('2002')]['Player'].count()
# len
result =len(df2[df2.Years.str.contains('2002')])
print(result)
이름에 ‘carlos’ 단어가 들어가는 선수의 숫자는 몇 명인가? (대, 소문자 구분 x)
# .lower()
len(df2[df2['Player'].str.lower().str.contains('carlos')])
월드컵 출전 횟수가 1회뿐인 선수들 중에서 가장 많은 득점을 올렸던 선수는 누구인가?
# s
df2[df2.LenCup == 1].sort_values('Goals',ascending=False)['Player'].iloc[0]
# .values[0]
df2[df2.LenCup==1].sort_values('Goals',ascending=False).Player.values[0]
# s
ans = df2[df2.LenCup == 1].groupby('Country').count().sort_values('Player',ascending=False)['Player'].index.tolist()[0]
print(ans)
# .index[0]
ans = df2[df2.LenCup == 1].groupby('Country').count().sort_values('Player',ascending=False)['Player'].index[0]
print(ans)