04wk-1: Pandas – 기본기능, missing, query, 할당, transform column

pandas
Author

최규빈

Published

September 25, 2023

1. 강의영상

2. Imports

import numpy as np
import pandas as pd

3. Pandas: 기본기능

A. 열의 이름 변경

- 방법1: df.columns에 대입

df = pd.DataFrame(np.random.randn(3,2))
df.columns = ['A','B']
df
A B
0 1.015321 -1.491732
1 -1.466425 -0.289752
2 -0.294463 0.641726

- 방법2: df.set_axis()

df = pd.DataFrame(np.random.randn(3,2))
df.set_axis(['A','B'],axis=1)
A B
0 -0.575323 -1.315989
1 0.077248 1.518059
2 0.018996 -0.818822

- 방법3: df.rename()

df = pd.DataFrame(np.random.randn(3,2))
#df.rename(columns={0:'AA',1:'BB'})
df.rename({0:'AA',1:'BB'},axis=1)
AA BB
0 -0.824893 0.530828
1 -0.964862 0.081930
2 -0.384178 -0.283328

B. 행의 이름 변경

- 방법1: df.index에 대입

df = pd.DataFrame(np.random.randn(3,2))
df
0 1
0 -1.140659 0.197573
1 -1.006808 0.495207
2 -0.546562 -0.263687
df.index = ['guebin','jiyoon','boram']
df
0 1
guebin -1.140659 0.197573
jiyoon -1.006808 0.495207
boram -0.546562 -0.263687

- 방법2: df.set_axis()

df = pd.DataFrame(np.random.randn(3,2))
df.set_axis([11,22,33],axis=0)
0 1
11 0.003062 0.005044
22 0.915182 0.303740
33 1.665422 -0.164989

- 방법3: df.rename()

df = pd.DataFrame(np.random.randn(3,2))
df.rename({1:'guebin'},axis=0)
0 1
0 0.937158 0.504850
guebin -0.891393 0.592022
2 1.386969 0.773002

- 방법4: 임의의 열을 행이름 으로 지정!

df = pd.DataFrame({'id':['2021-43052','2021-43052'], 'hour':[3,2], 'height':[176,172]})
df.set_index('id')
hour height
id
2021-43052 3 176
2021-43052 2 172

# A~B에 대한 연습문제

- 데이터 load

df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv')
df.head()
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN

5 rows × 29 columns

# 예제1: 열의 이름 출력하고, 열의 이름중 공백()이 있을 경우 언더바(_) 로 바꾸자.

컬럼출력

df.columns
Index(['ID', 'Name', 'Age', 'Photo', 'Nationality', 'Flag', 'Overall',
       'Potential', 'Club', 'Club Logo', 'Value', 'Wage', 'Special',
       'Preferred Foot', 'International Reputation', 'Weak Foot',
       'Skill Moves', 'Work Rate', 'Body Type', 'Real Face', 'Position',
       'Joined', 'Loaned From', 'Contract Valid Until', 'Height', 'Weight',
       'Release Clause', 'Kit Number', 'Best Overall Rating'],
      dtype='object')
  1. df.columns에 직접대입
new_colnames = [l.replace(' ','_') for l in df.columns]
# df.columns = new_colnames
# df
  1. set_axis() 이용
df.set_axis(new_colnames,axis=1)
ID Name Age Photo Nationality Flag Overall Potential Club Club_Logo ... Real_Face Position Joined Loaned_From Contract_Valid_Until Height Weight Release_Clause Kit_Number Best_Overall_Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 29 columns

  1. rename() 이용 – 안중요함
dct = {l:l.replace(' ','_') for l in df.columns}
df.rename(dct,axis=1)
ID Name Age Photo Nationality Flag Overall Potential Club Club_Logo ... Real_Face Position Joined Loaned_From Contract_Valid_Until Height Weight Release_Clause Kit_Number Best_Overall_Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 29 columns

#

예제2: ID를 row-index로 지정하라.

  1. 직접지정
# df.index = df.ID
# df
  1. set_axis()
df.set_axis(df.ID,axis=0)
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
ID
209658 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
212198 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
224334 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
192985 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
224232 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
269526 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
267946 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
270567 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
256624 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
256376 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 29 columns

  1. rename() – 안중요함
dct = {idx:ID for idx, ID in zip(df.index,df.ID)}
df.rename(dct,axis=0)
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
209658 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
212198 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
224334 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
192985 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
224232 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
269526 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
267946 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
270567 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
256624 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
256376 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 29 columns

  1. set_index()
df.set_index('ID')
Name Age Photo Nationality Flag Overall Potential Club Club Logo Value ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
ID
209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png €91M ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png €78.5M ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png €46.5M ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png €107.5M ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png €89.5M ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png €100K ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png €100K ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png €70K ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png €90K ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png €90K ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 28 columns

#

C. df.T

- 데이터 load

df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv')
df.head()
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN

5 rows × 29 columns

- df.T를 이용하여 데이터를 살피면 편리함

df.T.iloc[:,0:2]
0 1
ID 209658 212198
Name L. Goretzka Bruno Fernandes
Age 27 27
Photo https://cdn.sofifa.net/players/209/658/23_60.png https://cdn.sofifa.net/players/212/198/23_60.png
Nationality Germany Portugal
Flag https://cdn.sofifa.net/flags/de.png https://cdn.sofifa.net/flags/pt.png
Overall 87 86
Potential 88 87
Club FC Bayern München Manchester United
Club Logo https://cdn.sofifa.net/teams/21/30.png https://cdn.sofifa.net/teams/11/30.png
Value €91M €78.5M
Wage €115K €190K
Special 2312 2305
Preferred Foot Right Right
International Reputation 4.0 3.0
Weak Foot 4.0 3.0
Skill Moves 3.0 4.0
Work Rate High/ Medium High/ High
Body Type Unique Unique
Real Face Yes Yes
Position <span class="pos pos28">SUB <span class="pos pos15">LCM
Joined Jul 1, 2018 Jan 30, 2020
Loaned From NaN NaN
Contract Valid Until 2026 2026
Height 189cm 179cm
Weight 82kg 69kg
Release Clause €157M €155M
Kit Number 8.0 8.0
Best Overall Rating NaN NaN

- 출력옵션 조정

pd.options.display.max_rows = 12
display(df.T.iloc[:,:3])
pd.reset_option("display.max_rows")
0 1 2
ID 209658 212198 224334
Name L. Goretzka Bruno Fernandes M. Acuña
Age 27 27 30
Photo https://cdn.sofifa.net/players/209/658/23_60.png https://cdn.sofifa.net/players/212/198/23_60.png https://cdn.sofifa.net/players/224/334/23_60.png
Nationality Germany Portugal Argentina
... ... ... ...
Height 189cm 179cm 172cm
Weight 82kg 69kg 69kg
Release Clause €157M €155M €97.7M
Kit Number 8.0 8.0 19.0
Best Overall Rating NaN NaN NaN

29 rows × 3 columns

  • 이 예제에서는 줄이는 옵션을 사용했지만 보통은 늘려서 사용함

D. df.dtypes, s.dtype

- df.dtypes

df.dtypes
ID                            int64
Name                         object
Age                           int64
Photo                        object
Nationality                  object
Flag                         object
Overall                       int64
Potential                     int64
Club                         object
Club Logo                    object
Value                        object
Wage                         object
Special                       int64
Preferred Foot               object
International Reputation    float64
Weak Foot                   float64
Skill Moves                 float64
Work Rate                    object
Body Type                    object
Real Face                    object
Position                     object
Joined                       object
Loaned From                  object
Contract Valid Until         object
Height                       object
Weight                       object
Release Clause               object
Kit Number                  float64
Best Overall Rating          object
dtype: object

- s.dtype

df.Name.dtype
dtype('O')

- ==를 이용한 자료형 체크

df.Name.dtype == np.object_
True
df.Age.dtype == np.int64
True

# 예제: df에서 int64 자료형만 출력

- (풀이1)

pd.Series(list(df.dtypes))
0       int64
1      object
2       int64
3      object
4      object
5      object
6       int64
7       int64
8      object
9      object
10     object
11     object
12      int64
13     object
14    float64
15    float64
16    float64
17     object
18     object
19     object
20     object
21     object
22     object
23     object
24     object
25     object
26     object
27    float64
28     object
dtype: object
df.iloc[:,[0,2,6,7,12]]
ID Age Overall Potential Special
0 209658 27 87 88 2312
1 212198 27 86 87 2305
2 224334 30 85 85 2303
3 192985 31 91 91 2303
4 224232 25 86 89 2296
... ... ... ... ... ...
17655 269526 19 48 61 762
17656 267946 17 48 64 761
17657 270567 25 51 56 759
17658 256624 18 50 65 758
17659 256376 20 50 61 749

17660 rows × 5 columns

- (풀이2)

df.loc[:,[o == np.int64 for o in df.dtypes]]
ID Age Overall Potential Special
0 209658 27 87 88 2312
1 212198 27 86 87 2305
2 224334 30 85 85 2303
3 192985 31 91 91 2303
4 224232 25 86 89 2296
... ... ... ... ... ...
17655 269526 19 48 61 762
17656 267946 17 48 64 761
17657 270567 25 51 56 759
17658 256624 18 50 65 758
17659 256376 20 50 61 749

17660 rows × 5 columns

#

E. df.sort_values()

- 예시1: 나이가 어린 순서대로 정렬

df.sort_values(by='Age')
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
17636 263636 22 D. Oncescu 15 https://cdn.sofifa.net/players/263/636/22_60.png Romania https://cdn.sofifa.net/flags/ro.png 50 72 FC Dinamo 1948 Bucureşti https://cdn.sofifa.net/teams/100757/30.png ... No <span class="pos pos29">RES Jun 1, 2021 NaN 2025 190cm 77kg €306K 34.0 NaN
13712 271072 E. Topcu 16 https://cdn.sofifa.net/players/271/072/23_60.png Republic of Ireland https://cdn.sofifa.net/flags/ie.png 48 58 Drogheda United https://cdn.sofifa.net/teams/1572/30.png ... No <span class="pos pos29">RES Jul 8, 2022 NaN 2022 183cm 65kg €175K 20.0 NaN
13078 259442 22 R. van den Berg 16 https://cdn.sofifa.net/players/259/442/22_60.png Netherlands https://cdn.sofifa.net/flags/nl.png 60 81 PEC Zwolle https://cdn.sofifa.net/teams/1914/30.png ... No <span class="pos pos29">RES May 24, 2020 NaN 2024 190cm 73kg €1.8M 33.0 NaN
11257 266205 22 Y. Koré 16 https://cdn.sofifa.net/players/266/205/22_60.png France https://cdn.sofifa.net/flags/fr.png 59 74 Paris FC https://cdn.sofifa.net/teams/111817/30.png ... No <span class="pos pos29">RES Aug 11, 2022 NaN 2025 187cm 75kg €1.1M 34.0 NaN
11278 261873 21 H. Kumagai 16 https://cdn.sofifa.net/players/261/873/21_60.png Japan https://cdn.sofifa.net/flags/jp.png 52 70 Vegalta Sendai https://cdn.sofifa.net/teams/112836/30.png ... No <span class="pos pos29">RES Apr 16, 2021 NaN 2023 174cm 64kg €375K 48.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16311 254196 21 L. Fernández 42 https://cdn.sofifa.net/players/254/196/21_60.png Colombia https://cdn.sofifa.net/flags/co.png 61 61 Sociedad Deportiva Aucas https://cdn.sofifa.net/teams/110987/30.png ... No <span class="pos pos28">SUB Jan 29, 2018 NaN 2024 187cm 82kg €75K 1.0 NaN
16036 216692 S. Torrico 42 https://cdn.sofifa.net/players/216/692/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 72 72 San Lorenzo de Almagro https://cdn.sofifa.net/teams/1013/30.png ... No <span class="pos pos0">GK Apr 25, 2013 NaN 2022 183cm 84kg €375K 12.0 NaN
17257 645 17 D. Andersson 43 https://cdn.sofifa.net/players/000/645/17_60.png Sweden https://cdn.sofifa.net/flags/se.png 57 57 Helsingborgs IF https://cdn.sofifa.net/teams/432/30.png ... No <span class="pos pos28">SUB Apr 21, 2016 NaN 2022 187cm 85kg NaN 39.0 NaN
15375 1179 G. Buffon 44 https://cdn.sofifa.net/players/001/179/23_60.png Italy https://cdn.sofifa.net/flags/it.png 79 79 Parma https://cdn.sofifa.net/teams/50/30.png ... Yes <span class="pos pos0">GK Jul 1, 2021 NaN 2024 192cm 92kg €3M 1.0 NaN
15272 254704 22 K. Miura 54 https://cdn.sofifa.net/players/254/704/22_60.png Japan https://cdn.sofifa.net/flags/jp.png 56 56 Yokohama FC https://cdn.sofifa.net/teams/113197/30.png ... No <span class="pos pos29">RES Jul 1, 2005 NaN 2022 177cm 72kg NaN 11.0 NaN

17660 rows × 29 columns

- 예시2: 나이가 많은 순서대로 정렬

df.sort_values(by='Age',ascending=False)
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
15272 254704 22 K. Miura 54 https://cdn.sofifa.net/players/254/704/22_60.png Japan https://cdn.sofifa.net/flags/jp.png 56 56 Yokohama FC https://cdn.sofifa.net/teams/113197/30.png ... No <span class="pos pos29">RES Jul 1, 2005 NaN 2022 177cm 72kg NaN 11.0 NaN
15375 1179 G. Buffon 44 https://cdn.sofifa.net/players/001/179/23_60.png Italy https://cdn.sofifa.net/flags/it.png 79 79 Parma https://cdn.sofifa.net/teams/50/30.png ... Yes <span class="pos pos0">GK Jul 1, 2021 NaN 2024 192cm 92kg €3M 1.0 NaN
17257 645 17 D. Andersson 43 https://cdn.sofifa.net/players/000/645/17_60.png Sweden https://cdn.sofifa.net/flags/se.png 57 57 Helsingborgs IF https://cdn.sofifa.net/teams/432/30.png ... No <span class="pos pos28">SUB Apr 21, 2016 NaN 2022 187cm 85kg NaN 39.0 NaN
16036 216692 S. Torrico 42 https://cdn.sofifa.net/players/216/692/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 72 72 San Lorenzo de Almagro https://cdn.sofifa.net/teams/1013/30.png ... No <span class="pos pos0">GK Apr 25, 2013 NaN 2022 183cm 84kg €375K 12.0 NaN
16311 254196 21 L. Fernández 42 https://cdn.sofifa.net/players/254/196/21_60.png Colombia https://cdn.sofifa.net/flags/co.png 61 61 Sociedad Deportiva Aucas https://cdn.sofifa.net/teams/110987/30.png ... No <span class="pos pos28">SUB Jan 29, 2018 NaN 2024 187cm 82kg €75K 1.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17360 261023 21 H. Broun 16 https://cdn.sofifa.net/players/261/023/21_60.png Scotland https://cdn.sofifa.net/flags/gb-sct.png 52 72 Kilmarnock https://cdn.sofifa.net/teams/82/30.png ... No <span class="pos pos29">RES Sep 17, 2020 NaN 2022 182cm 70kg €523K 40.0 NaN
15536 263639 22 M. Pavel 16 https://cdn.sofifa.net/players/263/639/22_60.png Romania https://cdn.sofifa.net/flags/ro.png 51 69 FC Dinamo 1948 Bucureşti https://cdn.sofifa.net/teams/100757/30.png ... No <span class="pos pos29">RES Jul 1, 2021 NaN 2023 178cm 66kg €277K 77.0 NaN
11398 256405 21 W. Essanoussi 16 https://cdn.sofifa.net/players/256/405/21_60.png Netherlands https://cdn.sofifa.net/flags/nl.png 59 75 VVV-Venlo https://cdn.sofifa.net/teams/100651/30.png ... No <span class="pos pos29">RES Jul 1, 2019 NaN 2022 178cm 70kg €1.1M 24.0 NaN
15030 270594 T. Walczak 16 https://cdn.sofifa.net/players/270/594/23_60.png Poland https://cdn.sofifa.net/flags/pl.png 54 68 Wisła Płock https://cdn.sofifa.net/teams/1569/30.png ... No <span class="pos pos29">RES Sep 7, 2021 NaN 2023 191cm 88kg €494K 99.0 NaN
17636 263636 22 D. Oncescu 15 https://cdn.sofifa.net/players/263/636/22_60.png Romania https://cdn.sofifa.net/flags/ro.png 50 72 FC Dinamo 1948 Bucureşti https://cdn.sofifa.net/teams/100757/30.png ... No <span class="pos pos29">RES Jun 1, 2021 NaN 2025 190cm 77kg €306K 34.0 NaN

17660 rows × 29 columns

- 예시3: 능력치가 좋은 순서대로 정렬

df.sort_values(by = 'Overall',ascending=False)
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
41 188545 R. Lewandowski 33 https://cdn.sofifa.net/players/188/545/23_60.png Poland https://cdn.sofifa.net/flags/pl.png 91 91 FC Barcelona https://cdn.sofifa.net/teams/241/30.png ... Yes <span class="pos pos25">ST Jul 18, 2022 NaN 2025 185cm 81kg €172.2M 9.0 NaN
124 165153 K. Benzema 34 https://cdn.sofifa.net/players/165/153/23_60.png France https://cdn.sofifa.net/flags/fr.png 91 91 Real Madrid CF https://cdn.sofifa.net/teams/243/30.png ... Yes <span class="pos pos21">CF Jul 9, 2009 NaN 2023 185cm 81kg €131.2M 9.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
56 158023 L. Messi 35 https://cdn.sofifa.net/players/158/023/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 91 91 Paris Saint-Germain https://cdn.sofifa.net/teams/73/30.png ... Yes <span class="pos pos23">RW Aug 10, 2021 NaN 2023 169cm 67kg €99.9M 30.0 NaN
75 231747 K. Mbappé 23 https://cdn.sofifa.net/players/231/747/23_60.png France https://cdn.sofifa.net/flags/fr.png 91 95 Paris Saint-Germain https://cdn.sofifa.net/teams/73/30.png ... Yes <span class="pos pos25">ST Jul 1, 2018 NaN 2025 182cm 73kg €366.7M 7.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
15513 266751 22 Jung Ho Yeon 20 https://cdn.sofifa.net/players/266/751/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 45 53 GwangJu FC https://cdn.sofifa.net/teams/112258/30.png ... No <span class="pos pos29">RES Jan 20, 2022 NaN 2026 180cm 73kg €145K 23.0 NaN
16215 268279 22 J. Looschen 24 https://cdn.sofifa.net/players/268/279/22_60.png Germany https://cdn.sofifa.net/flags/de.png 44 47 SV Meppen https://cdn.sofifa.net/teams/110597/30.png ... No <span class="pos pos29">RES Mar 19, 2022 NaN 2026 178cm 78kg €92K 42.0 NaN
16042 255283 20 Kim Yeong Geun 22 https://cdn.sofifa.net/players/255/283/20_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 44 49 Gyeongnam FC https://cdn.sofifa.net/teams/111588/30.png ... No <span class="pos pos29">RES Jan 9, 2020 NaN 2020 174cm 71kg €53K 43.0 NaN
14634 269038 22 Zhang Wenxuan 16 https://cdn.sofifa.net/players/269/038/22_60.png China PR https://cdn.sofifa.net/flags/cn.png 44 59 Guangzhou FC https://cdn.sofifa.net/teams/111839/30.png ... No <span class="pos pos29">RES May 1, 2022 NaN 2022 175cm 70kg €239K 29.0 NaN
17618 168933 07 I. Paskov 33 https://cdn.sofifa.net/players/168/933/07_60.png Bulgaria https://cdn.sofifa.net/flags/bg.png 43 42 NaN https://cdn.sofifa.net/flags/bg.png ... NaN <span class="pos pos28">SUB NaN NaN NaN 184cm 79kg NaN 24.0 NaN

17660 rows × 29 columns

F. df.info()

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 17660 entries, 0 to 17659
Data columns (total 29 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   ID                        17660 non-null  int64  
 1   Name                      17660 non-null  object 
 2   Age                       17660 non-null  int64  
 3   Photo                     17660 non-null  object 
 4   Nationality               17660 non-null  object 
 5   Flag                      17660 non-null  object 
 6   Overall                   17660 non-null  int64  
 7   Potential                 17660 non-null  int64  
 8   Club                      17449 non-null  object 
 9   Club Logo                 17660 non-null  object 
 10  Value                     17660 non-null  object 
 11  Wage                      17660 non-null  object 
 12  Special                   17660 non-null  int64  
 13  Preferred Foot            17660 non-null  object 
 14  International Reputation  17660 non-null  float64
 15  Weak Foot                 17660 non-null  float64
 16  Skill Moves               17660 non-null  float64
 17  Work Rate                 17660 non-null  object 
 18  Body Type                 17622 non-null  object 
 19  Real Face                 17622 non-null  object 
 20  Position                  17625 non-null  object 
 21  Joined                    16562 non-null  object 
 22  Loaned From               694 non-null    object 
 23  Contract Valid Until      17299 non-null  object 
 24  Height                    17660 non-null  object 
 25  Weight                    17660 non-null  object 
 26  Release Clause            16509 non-null  object 
 27  Kit Number                17625 non-null  float64
 28  Best Overall Rating       21 non-null     object 
dtypes: float64(4), int64(5), object(20)
memory usage: 3.9+ MB

G. df.isna()

- 예시1: 열별로 결측치 count

df.isna().sum(axis=0)
ID                              0
Name                            0
Age                             0
Photo                           0
Nationality                     0
Flag                            0
Overall                         0
Potential                       0
Club                          211
Club Logo                       0
Value                           0
Wage                            0
Special                         0
Preferred Foot                  0
International Reputation        0
Weak Foot                       0
Skill Moves                     0
Work Rate                       0
Body Type                      38
Real Face                      38
Position                       35
Joined                       1098
Loaned From                 16966
Contract Valid Until          361
Height                          0
Weight                          0
Release Clause               1151
Kit Number                     35
Best Overall Rating         17639
dtype: int64

- 예시2: 결측치가 50% 이상인 열 출력

df.loc[:,df.isna().sum(axis=0) / len(df) > 0.5] 
Loaned From Best Overall Rating
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
... ... ...
17655 NaN NaN
17656 NaN NaN
17657 NaN NaN
17658 NaN NaN
17659 NaN NaN

17660 rows × 2 columns

axis 사용하기 어렵다면 https://guebin.github.io/PP2023/posts/02_DataScience/2023-04-12-6wk-2.html 를 복습할 것!

H. df.drop()

- 예시1: [0,1,2,3] 행을 drop

df.drop([0,1,2,3],axis=0)
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
5 212622 J. Kimmich 27 https://cdn.sofifa.net/players/212/622/23_60.png Germany https://cdn.sofifa.net/flags/de.png 89 90 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos9">RDM Jul 1, 2015 NaN 2025 177cm 75kg €182M 6.0 NaN
6 197445 D. Alaba 30 https://cdn.sofifa.net/players/197/445/23_60.png Austria https://cdn.sofifa.net/flags/at.png 86 86 Real Madrid CF https://cdn.sofifa.net/teams/243/30.png ... Yes <span class="pos pos6">LCB Jul 1, 2021 NaN 2026 180cm 78kg €113.8M 4.0 NaN
7 187961 22 Paulinho 32 https://cdn.sofifa.net/players/187/961/22_60.png Brazil https://cdn.sofifa.net/flags/br.png 83 83 Al Ahli https://cdn.sofifa.net/teams/112387/30.png ... Yes <span class="pos pos15">LCM Jul 22, 2021 NaN 2024 183cm 80kg €48.5M 15.0 NaN
8 208333 E. Can 28 https://cdn.sofifa.net/players/208/333/23_60.png Germany https://cdn.sofifa.net/flags/de.png 82 82 Borussia Dortmund https://cdn.sofifa.net/teams/22/30.png ... Yes <span class="pos pos28">SUB Feb 18, 2020 NaN 2024 186cm 86kg €51.9M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17656 rows × 29 columns

- 예시2: [‘Name’, ‘Age’] 열을 drop

df.drop(['Name','Age'],axis=1)
ID Photo Nationality Flag Overall Potential Club Club Logo Value Wage ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png €91M €115K ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png €78.5M €190K ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png €46.5M €46K ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png €107.5M €350K ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png €89.5M €110K ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png €100K €500 ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
17656 267946 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png €100K €500 ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
17657 270567 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png €70K €2K ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
17658 256624 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png €90K €500 ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
17659 256376 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png €90K €500 ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 27 columns

# G~H 에 대한 연습문제

# 예제: 결측치가 50퍼 이상인 열을 제외하라.

- (풀이1)

df.isna().mean(axis=0) > 0.5
ID                          False
Name                        False
Age                         False
Photo                       False
Nationality                 False
Flag                        False
Overall                     False
Potential                   False
Club                        False
Club Logo                   False
Value                       False
Wage                        False
Special                     False
Preferred Foot              False
International Reputation    False
Weak Foot                   False
Skill Moves                 False
Work Rate                   False
Body Type                   False
Real Face                   False
Position                    False
Joined                      False
Loaned From                  True
Contract Valid Until        False
Height                      False
Weight                      False
Release Clause              False
Kit Number                  False
Best Overall Rating          True
dtype: bool
df.drop(['Loaned From','Best Overall Rating'],axis=1)
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Work Rate Body Type Real Face Position Joined Contract Valid Until Height Weight Release Clause Kit Number
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... High/ Medium Unique Yes <span class="pos pos28">SUB Jul 1, 2018 2026 189cm 82kg €157M 8.0
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... High/ High Unique Yes <span class="pos pos15">LCM Jan 30, 2020 2026 179cm 69kg €155M 8.0
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... High/ High Stocky (170-185) No <span class="pos pos7">LB Sep 14, 2020 2024 172cm 69kg €97.7M 19.0
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... High/ High Unique Yes <span class="pos pos13">RCM Aug 30, 2015 2025 181cm 70kg €198.9M 17.0
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... High/ High Normal (170-) Yes <span class="pos pos13">RCM Sep 1, 2020 2026 172cm 68kg €154.4M 23.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... Medium/ Medium Normal (185+) No <span class="pos pos29">RES Apr 11, 2022 2027 190cm 78kg €218K 35.0
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... Medium/ Medium Lean (185+) No <span class="pos pos29">RES Jan 1, 2022 2026 195cm 84kg €188K 21.0
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... Medium/ Medium Lean (185+) No <span class="pos pos29">RES Jun 6, 2021 2023 190cm 82kg €142K 12.0
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... Medium/ Medium Normal (185+) No <span class="pos pos29">RES Jan 1, 2020 2021 187cm 79kg €214K 40.0
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... Medium/ Medium Normal (185+) No <span class="pos pos29">RES Jan 8, 2020 2021 186cm 78kg €131K 30.0

17660 rows × 27 columns

- (풀이2)

df.loc[:,df.isna().mean() < 0.5]
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Work Rate Body Type Real Face Position Joined Contract Valid Until Height Weight Release Clause Kit Number
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... High/ Medium Unique Yes <span class="pos pos28">SUB Jul 1, 2018 2026 189cm 82kg €157M 8.0
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... High/ High Unique Yes <span class="pos pos15">LCM Jan 30, 2020 2026 179cm 69kg €155M 8.0
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... High/ High Stocky (170-185) No <span class="pos pos7">LB Sep 14, 2020 2024 172cm 69kg €97.7M 19.0
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... High/ High Unique Yes <span class="pos pos13">RCM Aug 30, 2015 2025 181cm 70kg €198.9M 17.0
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... High/ High Normal (170-) Yes <span class="pos pos13">RCM Sep 1, 2020 2026 172cm 68kg €154.4M 23.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... Medium/ Medium Normal (185+) No <span class="pos pos29">RES Apr 11, 2022 2027 190cm 78kg €218K 35.0
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... Medium/ Medium Lean (185+) No <span class="pos pos29">RES Jan 1, 2022 2026 195cm 84kg €188K 21.0
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... Medium/ Medium Lean (185+) No <span class="pos pos29">RES Jun 6, 2021 2023 190cm 82kg €142K 12.0
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... Medium/ Medium Normal (185+) No <span class="pos pos29">RES Jan 1, 2020 2021 187cm 79kg €214K 40.0
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... Medium/ Medium Normal (185+) No <span class="pos pos29">RES Jan 8, 2020 2021 186cm 78kg €131K 30.0

17660 rows × 27 columns

#

4. Pandas: missing

A. Numpy

- 발생: np.nan

np.nan
nan
[1,2,3,np.nan]
[1, 2, 3, nan]
arr = np.array([1,2,3,np.nan])
arr
array([ 1.,  2.,  3., nan])

- np.array에 nan이 있으면 연산결과도 nan

arr.mean()
nan

- type

type(np.nan) 
float
type(arr[-1])
numpy.float64

B. Pandas

- 발생: np.nan, pd.NA

pd.Series([np.nan,1,2,3])
0    NaN
1    1.0
2    2.0
3    3.0
dtype: float64
pd.Series([pd.NA,1,2,3])
0    <NA>
1       1
2       2
3       3
dtype: object

- pd.Series에 NaN 혹은 <NA> 가 있다면 연산할때 제외함

pd.Series([np.nan,1,2,3]).mean()
2.0
pd.Series([pd.NA,1,2,3]).mean()
2.0

- type

s1 = pd.Series([np.nan,1,2,3])
type(s1[0])
numpy.float64
s2 = pd.Series([pd.NA,1,2,3])
type(s2[0])
pandas._libs.missing.NAType

- 검출 (\(\star\))

s1.isna()
0     True
1    False
2    False
3    False
dtype: bool
s2.isna()
0     True
1    False
2    False
3    False
dtype: bool
s1[0]
nan
pd.isna(s1[0]), pd.isnull(s1[0])
(True, True)
pd.isna(s2[0]), pd.isnull(s2[0])
(True, True)
id(pd.isna), id(pd.isnull) # 같은함수
(140619808936864, 140619808936864)

5. Pandas: query

ts = pd.DataFrame(np.random.normal(size=(20,4)),columns=list('ABCD'),index=pd.date_range('20221226',periods=20)).assign(E=['A']*10+['B']*10)
ts
A B C D E
2022-12-26 -0.442492 0.068711 -0.088430 -0.598197 A
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2022-12-28 -1.632762 3.049806 -0.708102 -1.493035 A
2022-12-29 -0.466743 -0.837421 -0.951080 0.754957 A
2022-12-30 0.120598 0.394019 -0.380788 0.303970 A
2022-12-31 0.120938 0.524992 1.233363 -2.521042 A
2023-01-01 -0.513943 -1.126340 0.631580 -1.641169 A
2023-01-02 0.270796 -0.336293 -0.877458 -1.190673 A
2023-01-03 0.824015 -0.478758 0.062818 -0.985841 A
2023-01-04 -0.436274 0.991324 -0.641292 1.461917 A
2023-01-05 -0.507242 -0.965891 1.653297 -0.044152 B
2023-01-06 -0.639832 -1.100579 -1.488237 -1.281468 B
2023-01-07 -1.556254 1.480460 -0.251670 0.058071 B
2023-01-08 0.332219 1.901141 -0.052167 -0.064043 B
2023-01-09 0.144748 0.428578 -1.241336 -0.107484 B
2023-01-10 1.496267 -1.620263 0.255344 0.003755 B
2023-01-11 1.372299 -0.081760 -1.924613 0.311614 B
2023-01-12 1.973132 0.242329 1.320583 -1.615392 B
2023-01-13 0.998978 -1.223152 -0.623063 -0.271440 B
2023-01-14 0.291082 -0.536917 -0.768778 0.470998 B

A. 기본 query

- 예시1: A>0 and B<0

ts.query('A>0 and B<0')
A B C D E
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2023-01-02 0.270796 -0.336293 -0.877458 -1.190673 A
2023-01-03 0.824015 -0.478758 0.062818 -0.985841 A
2023-01-10 1.496267 -1.620263 0.255344 0.003755 B
2023-01-11 1.372299 -0.081760 -1.924613 0.311614 B
2023-01-13 0.998978 -1.223152 -0.623063 -0.271440 B
2023-01-14 0.291082 -0.536917 -0.768778 0.470998 B

- 예시2: A<B<C

ts.query('A<B<C')
A B C D E
2022-12-31 0.120938 0.524992 1.233363 -2.521042 A

- 예시3: (A+B)/2 > 0

ts.query('(A+B)/2>0')
A B C D E
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2022-12-28 -1.632762 3.049806 -0.708102 -1.493035 A
2022-12-30 0.120598 0.394019 -0.380788 0.303970 A
2022-12-31 0.120938 0.524992 1.233363 -2.521042 A
2023-01-03 0.824015 -0.478758 0.062818 -0.985841 A
2023-01-04 -0.436274 0.991324 -0.641292 1.461917 A
2023-01-08 0.332219 1.901141 -0.052167 -0.064043 B
2023-01-09 0.144748 0.428578 -1.241336 -0.107484 B
2023-01-11 1.372299 -0.081760 -1.924613 0.311614 B
2023-01-12 1.973132 0.242329 1.320583 -1.615392 B

- 예시4: (A+B)/2 > 0 and E=='A'

ts.query('(A+B)/2>0 and E=="A"')
A B C D E
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2022-12-28 -1.632762 3.049806 -0.708102 -1.493035 A
2022-12-30 0.120598 0.394019 -0.380788 0.303970 A
2022-12-31 0.120938 0.524992 1.233363 -2.521042 A
2023-01-03 0.824015 -0.478758 0.062818 -0.985841 A
2023-01-04 -0.436274 0.991324 -0.641292 1.461917 A
ts.query("(A+B)/2>0 and E=='A'")
A B C D E
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2022-12-28 -1.632762 3.049806 -0.708102 -1.493035 A
2022-12-30 0.120598 0.394019 -0.380788 0.303970 A
2022-12-31 0.120938 0.524992 1.233363 -2.521042 A
2023-01-03 0.824015 -0.478758 0.062818 -0.985841 A
2023-01-04 -0.436274 0.991324 -0.641292 1.461917 A

B. 외부변수를 이용

- 예시: A > mean(A)

value = np.percentile(ts.A,77) # ts.A 에서 77 percentile에 해당하는 숫자
ts.query("A > @value")
A B C D E
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2023-01-10 1.496267 -1.620263 0.255344 0.003755 B
2023-01-11 1.372299 -0.081760 -1.924613 0.311614 B
2023-01-12 1.973132 0.242329 1.320583 -1.615392 B
2023-01-13 0.998978 -1.223152 -0.623063 -0.271440 B
Note

명확한 전달을 위해서 영상에서 설명한 예제를 조금 바꿨습니다.

C. Index로 query

- 예시: (2022년 12월30일 보다 이전 날짜) \(\cup\) (2023년 1월10일)

ts.query('index < "2022-12-30" or index == "2023-01-10"')
A B C D E
2022-12-26 -0.442492 0.068711 -0.088430 -0.598197 A
2022-12-27 1.878222 -0.363973 0.652147 -2.757448 A
2022-12-28 -1.632762 3.049806 -0.708102 -1.493035 A
2022-12-29 -0.466743 -0.837421 -0.951080 0.754957 A
2023-01-10 1.496267 -1.620263 0.255344 0.003755 B

D. 열의 이름에 공백이 있을 경우

df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv')
df.head()
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN

5 rows × 29 columns

- 예시: Skill Moves > 4

df.query('`Skill Moves` > 4')
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
19 193082 J. Cuadrado 34 https://cdn.sofifa.net/players/193/082/23_60.png Colombia https://cdn.sofifa.net/flags/co.png 83 83 Juventus https://cdn.sofifa.net/teams/45/30.png ... Yes <span class="pos pos3">RB Jul 1, 2017 NaN 2023 179cm 72kg €23M 11.0 NaN
27 189509 Thiago 31 https://cdn.sofifa.net/players/189/509/23_60.png Spain https://cdn.sofifa.net/flags/es.png 86 86 Liverpool https://cdn.sofifa.net/teams/9/30.png ... Yes <span class="pos pos15">LCM Sep 18, 2020 NaN 2024 174cm 70kg €102.7M 6.0 NaN
44 232411 C. Nkunku 24 https://cdn.sofifa.net/players/232/411/23_60.png France https://cdn.sofifa.net/flags/fr.png 86 89 RB Leipzig https://cdn.sofifa.net/teams/112172/30.png ... Yes <span class="pos pos28">SUB NaN NaN NaN 175cm 73kg €166.9M 12.0 NaN
62 233927 Lucas Paquetá 24 https://cdn.sofifa.net/players/233/927/23_60.png Brazil https://cdn.sofifa.net/flags/br.png 82 87 Olympique Lyonnais https://cdn.sofifa.net/teams/66/30.png ... Yes <span class="pos pos15">LCM Oct 1, 2020 NaN 2025 180cm 72kg €90.9M 10.0 NaN
75 231747 K. Mbappé 23 https://cdn.sofifa.net/players/231/747/23_60.png France https://cdn.sofifa.net/flags/fr.png 91 95 Paris Saint-Germain https://cdn.sofifa.net/teams/73/30.png ... Yes <span class="pos pos25">ST Jul 1, 2018 NaN 2025 182cm 73kg €366.7M 7.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
4516 253755 Talles Magno 20 https://cdn.sofifa.net/players/253/755/23_60.png Brazil https://cdn.sofifa.net/flags/br.png 71 83 New York City FC https://cdn.sofifa.net/teams/112828/30.png ... No <span class="pos pos16">LM May 18, 2021 NaN 2026 186cm 70kg €7.7M 43.0 NaN
4643 246548 O. Sahraoui 21 https://cdn.sofifa.net/players/246/548/23_60.png Norway https://cdn.sofifa.net/flags/no.png 67 78 Vålerenga Fotball https://cdn.sofifa.net/teams/920/30.png ... No <span class="pos pos27">LW May 15, 2019 NaN 2023 170cm 65kg €3.3M 10.0 NaN
4872 251570 R. Cherki 18 https://cdn.sofifa.net/players/251/570/23_60.png France https://cdn.sofifa.net/flags/fr.png 73 88 Olympique Lyonnais https://cdn.sofifa.net/teams/66/30.png ... No <span class="pos pos28">SUB Jul 7, 2019 NaN 2023 176cm 71kg €17.7M 18.0 NaN
5361 225712 D. Bahamboula 27 https://cdn.sofifa.net/players/225/712/23_60.png Congo https://cdn.sofifa.net/flags/cg.png 63 63 Livingston FC https://cdn.sofifa.net/teams/621/30.png ... No <span class="pos pos28">SUB Jul 9, 2022 NaN 2024 185cm 70kg €875K 7.0 NaN
10452 212455 17 H. Mastour 18 https://cdn.sofifa.net/players/212/455/17_60.png Morocco https://cdn.sofifa.net/flags/ma.png 65 76 PEC Zwolle https://cdn.sofifa.net/teams/1914/30.png ... No <span class="pos pos28">SUB NaN <a href="/team/47/ac-milan/">AC Milan</a> Jun 30, 2017 175cm 63kg NaN 98.0 NaN

65 rows × 29 columns

6. Pandas: 할당

np.random.seed(43052)
att = np.random.choice(np.arange(10,21)*5,20)
rep = np.random.choice(np.arange(5,21)*5,20)
mid = np.random.choice(np.arange(0,21)*5,20)
fin = np.random.choice(np.arange(0,21)*5,20)
df = pd.DataFrame({'att':att,'rep':rep,'mid':mid,'fin':fin})
df
att rep mid fin
0 65 55 50 40
1 95 100 50 80
2 65 90 60 30
3 55 80 75 80
4 80 30 30 100
5 75 40 100 15
6 65 45 45 90
7 60 60 25 0
8 95 65 20 10
9 90 80 80 20
10 55 75 35 25
11 95 95 45 0
12 95 55 15 35
13 50 80 40 30
14 50 55 15 85
15 95 30 30 95
16 50 50 45 10
17 65 55 15 45
18 70 70 40 35
19 90 90 80 90

A. df.assign()

- 예시: total = att*0.1 + rep*0.2 + mid*0.35 + fin*0.35 를 계산하여 할당

df.assign(total = df.att*0.1 + df.rep*0.2 + df.mid*0.35 + df.fin*0.35)
att rep mid fin total
0 65 55 50 40 49.00
1 95 100 50 80 75.00
2 65 90 60 30 56.00
3 55 80 75 80 75.75
4 80 30 30 100 59.50
5 75 40 100 15 55.75
6 65 45 45 90 62.75
7 60 60 25 0 26.75
8 95 65 20 10 33.00
9 90 80 80 20 60.00
10 55 75 35 25 41.50
11 95 95 45 0 44.25
12 95 55 15 35 38.00
13 50 80 40 30 45.50
14 50 55 15 85 51.00
15 95 30 30 95 59.25
16 50 50 45 10 34.25
17 65 55 15 45 38.50
18 70 70 40 35 47.25
19 90 90 80 90 86.50

Note: 이 방법은 df를 일시적으로 변화시킴

B. df.eval()

- 예시: total = att*0.1 + rep*0.2 + mid*0.35 + fin*0.35 를 계산하여 할당

df.eval('total = att*0.1 + rep*0.2 + mid*0.35 + fin*0.35')
att rep mid fin total
0 65 55 50 40 49.00
1 95 100 50 80 75.00
2 65 90 60 30 56.00
3 55 80 75 80 75.75
4 80 30 30 100 59.50
5 75 40 100 15 55.75
6 65 45 45 90 62.75
7 60 60 25 0 26.75
8 95 65 20 10 33.00
9 90 80 80 20 60.00
10 55 75 35 25 41.50
11 95 95 45 0 44.25
12 95 55 15 35 38.00
13 50 80 40 30 45.50
14 50 55 15 85 51.00
15 95 30 30 95 59.25
16 50 50 45 10 34.25
17 65 55 15 45 38.50
18 70 70 40 35 47.25
19 90 90 80 90 86.50

Note: 이 방법은 df를 일시적으로 변화시킴

C. df[colname] = xxx

- 예시: total = att*0.1 + rep*0.2 + mid*0.35 + fin*0.35 를 계산하여 할당.

df['total'] = df.att*0.1 + df.rep*0.2 + df.mid*0.35 + df.fin*0.35
df
att rep mid fin total
0 65 55 50 40 49.00
1 95 100 50 80 75.00
2 65 90 60 30 56.00
3 55 80 75 80 75.75
4 80 30 30 100 59.50
5 75 40 100 15 55.75
6 65 45 45 90 62.75
7 60 60 25 0 26.75
8 95 65 20 10 33.00
9 90 80 80 20 60.00
10 55 75 35 25 41.50
11 95 95 45 0 44.25
12 95 55 15 35 38.00
13 50 80 40 30 45.50
14 50 55 15 85 51.00
15 95 30 30 95 59.25
16 50 50 45 10 34.25
17 65 55 15 45 38.50
18 70 70 40 35 47.25
19 90 90 80 90 86.50

Note: 이 방법은 df를 영구적으로 변화시킴

7. Pandas: transform column

A. lambda

- 예시1: \(x \to x+2\)

f = lambda x: x+2
f(1)
3

- 예시2: \(x,y \to x+y\)

f = lambda x,y: x+y
f(1,2)
3

- 예시3: ‘2023-09’ \(\to\) 9

f = lambda x: int(x[-2:])
f('2023-09')
9

- 예시4: ‘2023-09’ \(\to\) (2023,9)

f = lambda x: (int(x[:4]),int(x[-2:]))
f('2023-09')
(2023, 9)

- 예시5: 문자열이 ‘cat’이면 1 ’dog’ 이면 0 // ’cat이면 1 ’cat’이 아니면 0

f = lambda x: 1 if x=='cat' else 0
f('cat'), f('dog')
(1, 0)

- Note: f로 이름을 정하지 않고 직접 사용 가능

(lambda x: x+1)(2)
3

B. map

- 개념: map(f,[x1,x2,...xn])=[f(x1),f(x2),...,f(xn)]

- 예시1: x->x+1[1,2,3]에 적용

list(map(lambda x: x+1, [1,-5,3]))
[2, -4, 4]

- 예시2 df.Height열 변환하기

df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv')
s = df.Height[:5]
s
0    189cm
1    179cm
2    172cm
3    181cm
4    172cm
Name: Height, dtype: object
x = s[0]
x
'189cm'
list(map(lambda x: int(x.replace('cm','')), s))
[189, 179, 172, 181, 172]

# 예시3 df.Height열 변환하기 + 변환된 열 할당하기

df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv')

- (풀이1)

df.assign(Height= list(map(lambda x: int(x.replace('cm','')), df.Height)))
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186 78kg €131K 30.0 NaN

17660 rows × 29 columns

- (풀이2) – 사실 수틀리면 컴프리헨션 쓰면 된다.

df.assign(Height= [int(height.replace('cm','')) for height in df.Height])
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186 78kg €131K 30.0 NaN

17660 rows × 29 columns

#

# 예시4df.Position 열에 아래와 같은 변환을 수행하고, 변환된 열을 할당하라.

before after
<span class="pos pos28">SUB SUB
<span class="pos pos15">LCM LCM
<span class="pos pos7">LB LB
<span class="pos pos13">RCM RCM
<span class="pos pos13">RCM RCM
df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv')

- (풀이1)

x = df.Position[0]
x
'<span class="pos pos28">SUB'
df.assign(Position = list(map(lambda x: x.split('>')[-1] if not pd.isna(x) else 'NA', df.Position)))
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 29 columns

- (풀이2) – 리스트컴프리헨션

f = lambda x: x.split('>')[-1] if not pd.isna(x) else 'NA'
df.assign(Position = [f(x) for x in df.Position])
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Kit Number Best Overall Rating
0 209658 L. Goretzka 27 https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png 87 88 FC Bayern München https://cdn.sofifa.net/teams/21/30.png ... Yes SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M 8.0 NaN
1 212198 Bruno Fernandes 27 https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png 86 87 Manchester United https://cdn.sofifa.net/teams/11/30.png ... Yes LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M 8.0 NaN
2 224334 M. Acuña 30 https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png 85 85 Sevilla FC https://cdn.sofifa.net/teams/481/30.png ... No LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M 19.0 NaN
3 192985 K. De Bruyne 31 https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png 91 91 Manchester City https://cdn.sofifa.net/teams/10/30.png ... Yes RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M 17.0 NaN
4 224232 N. Barella 25 https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png 86 89 Inter https://cdn.sofifa.net/teams/44/30.png ... Yes RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M 23.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 269526 Deng Xiongtao 19 https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png 48 61 Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png ... No RES Apr 11, 2022 NaN 2027 190cm 78kg €218K 35.0 NaN
17656 267946 22 Lim Jun Sub 17 https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png 48 64 Jeju United FC https://cdn.sofifa.net/teams/1478/30.png ... No RES Jan 1, 2022 NaN 2026 195cm 84kg €188K 21.0 NaN
17657 270567 A. Demir 25 https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png 51 56 Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png ... No RES Jun 6, 2021 NaN 2023 190cm 82kg €142K 12.0 NaN
17658 256624 21 S. Czajor 18 https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png 50 65 Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png ... No RES Jan 1, 2020 NaN 2021 187cm 79kg €214K 40.0 NaN
17659 256376 21 F. Jakobsson 20 https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png 50 61 IFK Norrköping https://cdn.sofifa.net/teams/702/30.png ... No RES Jan 8, 2020 NaN 2021 186cm 78kg €131K 30.0 NaN

17660 rows × 29 columns

  • 개인의견: 이미 람다로 함수를 만들어야 해서 그냥 map을 쓰는게 자연스러움

8. HW

- 문제: 아래의 자료에서 입학년도를 추가하고 싶다면?

np.random.seed(43052)
att = np.random.choice(np.arange(10,21)*5,20)
rep = np.random.choice(np.arange(5,21)*5,20)
mid = np.random.choice(np.arange(0,21)*5,20)
fin = np.random.choice(np.arange(0,21)*5,20)
student_id = [ '2023-12362', '2022-12471', '2023-12333', '2022-12400', '2022-12377',
               '2022-12469', '2023-12314', '2022-12363', '2023-12445', '2023-12336',
               '2023-12426', '2022-12380', '2023-12422', '2022-12488', '2022-12370',
               '2023-12443', '2022-12463', '2023-12491', '2023-12340', '2022-12312' ]
df = pd.DataFrame({'student_id':student_id,'att':att,'rep':rep,'mid':mid,'fin':fin})
df.head()
student_id att rep mid fin
0 2023-12362 65 55 50 40
1 2022-12471 95 100 50 80
2 2023-12333 65 90 60 30
3 2022-12400 55 80 75 80
4 2022-12377 80 30 30 100
# 출력결과가 아래와 같아야 한다.
student_id att rep mid fin Year
0 2023-12362 65 55 50 40 2023
1 2022-12471 95 100 50 80 2022
2 2023-12333 65 90 60 30 2023
3 2022-12400 55 80 75 80 2022
4 2022-12377 80 30 30 100 2022
5 2022-12469 75 40 100 15 2022
6 2023-12314 65 45 45 90 2023
7 2022-12363 60 60 25 0 2022
8 2023-12445 95 65 20 10 2023
9 2023-12336 90 80 80 20 2023
10 2023-12426 55 75 35 25 2023
11 2022-12380 95 95 45 0 2022
12 2023-12422 95 55 15 35 2023
13 2022-12488 50 80 40 30 2022
14 2022-12370 50 55 15 85 2022
15 2023-12443 95 30 30 95 2023
16 2022-12463 50 50 45 10 2022
17 2023-12491 65 55 15 45 2023
18 2023-12340 70 70 40 35 2023
19 2022-12312 90 90 80 90 2022