월드컵 출전선수 골기록 데이터
import pandas as pd
df= pd.read_csv('https://raw.githubusercontent.com/Datamanim/datarepo/main/worldcup/worldcupgoals.csv')
df.head()
|
Player |
Goals |
Years |
Country |
0 |
Miroslav Klose |
16 |
2002-2006-2010-2014 |
Germany |
1 |
Ronaldo |
15 |
1998-2002-2006 |
Brazil |
2 |
Gerd Muller |
14 |
1970-1974 |
Germany |
3 |
Just Fontaine |
13 |
1958 |
France |
4 |
Pele |
12 |
1958-1962-1966-1970 |
Brazil |
Q23. Years 컬럼은 년도 -년도 형식으로 구성되어있고, 각 년도는 4자리 숫자이다. 년도 표기가 4자리 숫자로 안된 케이스가 존재한다. 해당 건은 몇건인지 출력하라
df['yearList'] = df.Years.str.split('-') # 각 원소는 list type
def check4(x) : # apply 로 해당 열의 원소가 차례로 들어간다.
for i in x :
if len(str(i)) != 4 :
return False
return True # if 문 안에 있으면 첫 원소만 비교하고, 밖에 있으면 전체 list 와 비교
df['check'] = df.yearList.apply(check4)
# series 일때는 자동으로 각 원소를 차례로 넣고, dataframe일 떄는 axis 설정 필요
len(df[df.check == False])
만약, 쪼개어져 있는 list로 구성된 data를 어떻게 합칠 수 있지? (join)
|
Player |
Goals |
Years |
Country |
yearList |
check |
0 |
Miroslav Klose |
16 |
2002-2006-2010-2014 |
Germany |
[2002, 2006, 2010, 2014] |
True |
1 |
Ronaldo |
15 |
1998-2002-2006 |
Brazil |
[1998, 2002, 2006] |
True |
2 |
Gerd Muller |
14 |
1970-1974 |
Germany |
[1970, 1974] |
True |
3 |
Just Fontaine |
13 |
1958 |
France |
[1958] |
True |
4 |
Pele |
12 |
1958-1962-1966-1970 |
Brazil |
[1958, 1962, 1966, 1970] |
True |
df['test1'] = df.yearList.apply(lambda x : ','.join(map(str, x)))
# '구분자'.join(x) 형태로 작성
# map(변환함수, 반복객체)
|
Player |
Goals |
Years |
Country |
yearList |
check |
test1 |
0 |
Miroslav Klose |
16 |
2002-2006-2010-2014 |
Germany |
[2002, 2006, 2010, 2014] |
True |
2002,2006,2010,2014 |
1 |
Ronaldo |
15 |
1998-2002-2006 |
Brazil |
[1998, 2002, 2006] |
True |
1998,2002,2006 |
2 |
Gerd Muller |
14 |
1970-1974 |
Germany |
[1970, 1974] |
True |
1970,1974 |
3 |
Just Fontaine |
13 |
1958 |
France |
[1958] |
True |
1958 |
4 |
Pele |
12 |
1958-1962-1966-1970 |
Brazil |
[1958, 1962, 1966, 1970] |
True |
1958,1962,1966,1970 |
28. 이름에 ‘carlos’ 단어가 들어가는 선수의 숫자는 몇 명인가? (대, 소문자 구분 x)
len(df[df.Player.str.lower().str.contains('carlos')])
# 미리 소문자로 전체적으로 다 바꾸고, 그 다음에 소문자와 비교
Leave a comment