05_Pandas

05-2_연습문제_euro2012

chuuvelop 2025. 3. 6. 21:40
728x90
import pandas as pd

 

문제1. Euro_2012_stats_TEAM.csv 데이터 읽어오기

euro = pd.read_csv("./data/Euro_2012_stats_TEAM.csv")
euro.head()

 

 

문제2. Goals 컬럼 추출

euro["Goals"]
0      4
1      4
2      4
3      5
4      3
5     10
6      5
7      6
8      2
9      2
10     6
11     1
12     5
13    12
14     5
15     2
Name: Goals, dtype: int64

 

 

문제3. 참가팀 수 확인하기

len(euro["Team"])

# len(euro)
# euro.shape[0]
# euro["Team"].nunique()
16

 

 

문제4. 컬럼 수 확인

len(euro.columns)
# euro.shape[1]
35

 

 

문제5. Team, Red Cards, Yellow Cards 열을 추출해 discipline 변수에 저장

euro.columns
Index(['Team', 'Goals', 'Shots on target', 'Shots off target',
       'Shooting Accuracy', '% Goals-to-shots', 'Total shots (inc. Blocked)',
       'Hit Woodwork', 'Penalty goals', 'Penalties not scored', 'Headed goals',
       'Passes', 'Passes completed', 'Passing Accuracy', 'Touches', 'Crosses',
       'Dribbles', 'Corners Taken', 'Tackles', 'Clearances', 'Interceptions',
       'Clearances off line', 'Clean Sheets', 'Blocks', 'Goals conceded',
       'Saves made', 'Saves-to-shots ratio', 'Fouls Won', 'Fouls Conceded',
       'Offsides', 'Yellow Cards', 'Red Cards', 'Subs on', 'Subs off',
       'Players Used'],
      dtype='object')
discipline = euro[["Team","Red Cards","Yellow Cards"]]
discipline.head()

 

 

문제6. Red Cards와 Yellow Cards의 내림차순으로 정렬한 결과를 출력

discipline.sort_values(["Red Cards" , "Yellow Cards"] , ascending = False)

 

문제7. Yellow Cards 열의 평균 구하기

  • 단, 소수점이하는 반올림
round(discipline["Yellow Cards"].mean())
7

 

문제8. Goals가 6 초과인 팀을 필터링

euro[euro["Goals"] > 6]

 

 

문제9. 팀이름이 G로 시작하는 팀 필터링

euro[euro["Team"].map(lambda x: x[0] == "G")]

 

 

euro[euro["Team"].str.startswith("G")]

 

 

문제10. 일곱번째 컬럼까지 추출

df.iloc[:, :7]

 

 

문제11. 오른쪽 끝 3개 열을 제외하고 추출

euro.iloc[:, :-3]

 

 

문제12. England, Italy, Russia팀의 Team, Shooting Accuracy 열을 추출

euro.head()

 

 

euro.loc[euro["Team"].isin(["England", "Italy", "Russia"]), ["Team", "Shooting Accuracy"]]

728x90

'05_Pandas' 카테고리의 다른 글

05-4_연습문제_Vaccine  (0) 2025.03.07
05-3_연습문제_Fictional_Army  (1) 2025.03.06
05-1_연습문제_student_alchol_consumption  (0) 2025.03.06
05_데이터프레임 응용  (0) 2025.03.06
04-1_연습문제_iris  (0) 2025.03.06