728x90
반응형
Step 1. Import the necessary libraries
->import pandas as pd
Step 2. Import the dataset from this address.
Step 3. Assign it to a variable called euro12.
-> euro12 = pd.read_csv('https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/02_Filtering_%26_Sorting/Euro12/Euro_2012_stats_TEAM.csv', sep=',')
euro12
Step 4. Select only the Goal column.
-> euro12.Goals
A : 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
Step 5. How many team participated in the Euro2012?
-> euro12.Team.value_counts().count()
#or
euro12.shape[0]
A : 16
Step 6. What is the number of columns in the dataset?
-> euro12.shape[1]
A : 35
Step 7. View only the columns Team, Yellow Cards and Red Cards and assign them to a dataframe called discipline
-> discipline = euro12[['Team','Yellow Cards','Red Cards']]
discipline
A :
Step 8. Sort the teams by Red Cards, then to Yellow Cards
-> discipline.sort_values(by=['Red Cards','Yellow Cards'],ascending=False)
A :
해설 : sort_values()로 indexing한다. by를 통해 기준을 정함
Step 9. Calculate the mean Yellow Cards given per Team
-> round(discipline['Yellow Cards'].mean())
A : 7
해설 : round로 소수점을 반환한다.
Step 10. Filter teams that scored more than 6 goals
-> euro12[euro12['Goals']>6]
A:
Step 11. Select the teams that start with G
-> euro12[euro12.Team.str.startswith("G")]
A :
해설 : string을 고르고 startwith()로 indexing 한다.
Step 12. Select the first 7 columns
-> Euro12.iloc[ : , 0:7 ]
A :
해설 : slicing하기 위해 Iloc을 사용한다. : 는 전체를 0:7은 0~7을 의미한다. Col = 7
Step 13. Select all columns except the last 3.
-> euro12[:-3]
#or
euro12.iloc[: , :-3]
해설 : col = 32개 나옴. -로 지정해서 마지막 3개를 제외시켰다.
Step 14. Present only the Shooting Accuracy from England, Italy and Russia
-> euro12.loc[euro12.Team.isin(['England', 'Italy', 'Russia']), ['Team','Shooting Accuracy']]
A:
해설 : loc은 col의 label로 slicing하는 방법이다. isin으로 col을 선택하였다.
728x90
반응형