10wk-2: pandas, 반복문자투리, 변수의범위

Author

최규빈

Published

May 10, 2024

1. 강의영상

2. Imports

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import itertools

3. Pandas

A. numpy 대비 장점?

- 장점1: 모든 자료형이 동일할 필요가 없다.

(문제) – 아래의 리스트에서 능력치가 90 보다 큰 선수들을 출력하라.

lst = [['Bruno Fernandes', 'Europe', 88],
       ['L. Goretzka', 'Europe', 87],
       ['L. Suárez', 'South America', 88],
       ['K. De Bruyne', 'Europe', 91],
       ['M. Acuña', 'South America', 84],
       ['J. Kimmich', 'Europe', 89]]

(풀이) – 아마 이렇게..

[[name,continent,x] for name, continent, x in lst if x>90]
[['K. De Bruyne', 'Europe', 91]]

(풀이) – 말린다면..

arr = np.array(lst)
arr
array([['Bruno Fernandes', 'Europe', '88'],
       ['L. Goretzka', 'Europe', '87'],
       ['L. Suárez', 'South America', '88'],
       ['K. De Bruyne', 'Europe', '91'],
       ['M. Acuña', 'South America', '84'],
       ['J. Kimmich', 'Europe', '89']], dtype='<U21')
arr[arr[:,2] > 90]
UFuncTypeError: ufunc 'greater' did not contain a loop with signature matching types (<class 'numpy.dtypes.StrDType'>, <class 'numpy.dtypes.Int64DType'>) -> <class 'numpy.dtypes.BoolDType'>

(풀이) – 판다스였다면

df = pd.DataFrame(lst)
df
0 1 2
0 Bruno Fernandes Europe 88
1 L. Goretzka Europe 87
2 L. Suárez South America 88
3 K. De Bruyne Europe 91
4 M. Acuña South America 84
5 J. Kimmich Europe 89
df[df.iloc[:,2] > 90]
0 1 2
3 K. De Bruyne Europe 91

(분석)

arr[:,0],arr[:,1],arr[:,2] # 문자열,문자열,문자열 -> 각 col의 자료형이 반드시 같아야함
(array(['Bruno Fernandes', 'L. Goretzka', 'L. Suárez', 'K. De Bruyne',
        'M. Acuña', 'J. Kimmich'], dtype='<U21'),
 array(['Europe', 'Europe', 'South America', 'Europe', 'South America',
        'Europe'], dtype='<U21'),
 array(['88', '87', '88', '91', '84', '89'], dtype='<U21'))
df.iloc[:,0], df.iloc[:,1], df.iloc[:,2] # object,object,int -> 각 col의 자료형이 다를수도있음.
(0    Bruno Fernandes
 1        L. Goretzka
 2          L. Suárez
 3       K. De Bruyne
 4           M. Acuña
 5         J. Kimmich
 Name: 0, dtype: object,
 0           Europe
 1           Europe
 2    South America
 3           Europe
 4    South America
 5           Europe
 Name: 1, dtype: object,
 0    88
 1    87
 2    88
 3    91
 4    84
 5    89
 Name: 2, dtype: int64)

- 장점2: 인덱싱과 해싱을 동시에 할 수 있다. (순서형자료형과 맵핑형자료형의 장점을 동시에 가짐)

  • list의 장점 \(\to\) numpy 가 계승
  • dct의 장점 \(\to\) pandas 가 계승 + list/numpy의 장점 일부흡수
dct = {'att':[30,40,50],'mid':[50,60,70]}
df = pd.DataFrame(dct)
df
att mid
0 30 50
1 40 60
2 50 70
df['att'], dct['att'] # 해싱
(0    30
 1    40
 2    50
 Name: att, dtype: int64,
 [30, 40, 50])
df.iloc[[1],[0,1]] # 인덱싱
att mid
1 40 60

느낌: 판다스데이터프레임은 엑셀 그자체!

B. 선언방법

- 방법1: dictionary에서 만든다. (맵핑형자료에서 생성)

pd.DataFrame({'att':[30,40,50],'mid':[50,60,70]})
att mid
0 30 50
1 40 60
2 50 70
pd.DataFrame({'att':(30,40,50),'mid':(50,60,70)})
att mid
0 30 50
1 40 60
2 50 70
pd.DataFrame({'att':np.array([30,40,50]),'mid':np.array([50,60,70])})
att mid
0 30 50
1 40 60
2 50 70

- 방법2: ndarray 혹은 list 등에서 만든다. (보통 2차원의 중첩구조를 가지는 순서형자료에서 생성하는게 일반적임)

np.arange(2*3).reshape(2,3)
array([[0, 1, 2],
       [3, 4, 5]])
pd.DataFrame(np.arange(2*3).reshape(2,3))
0 1 2
0 0 1 2
1 3 4 5

C. 자료형, len, shape, for문의 반복변수

df = pd.DataFrame({'att':[30,40,50],'mid':[5,45,90]})
df
att mid
0 30 5
1 40 45
2 50 90

- type

type(df)
pandas.core.frame.DataFrame

- len

len(df) # row의 갯수 
3

- shape

df.shape 
(3, 2)

- for문의 반복변수

for k in df:
    print(k) # 딕셔너리같죠
att
mid
for k in {'att':[30,40,50],'mid':[5,45,90]}: 
    print(k)
att
mid

참고: df는 진짜 딕셔너리 느낌 강해요

df.keys()
Index(['att', 'mid'], dtype='object')
for k,v in df.items():
    print(k)
    print(v)
    print('\n')
att
0    30
1    40
2    50
Name: att, dtype: int64


mid
0     5
1    45
2    90
Name: mid, dtype: int64

D. pd.Series

- 2차원 ndarray가 pd.DataFrame에 대응한다면 1차원 ndarray는 pd.Series에 대응한다.

s=pd.Series(np.random.randn(10))
s
0    0.252373
1   -1.075307
2   -0.368705
3    0.784600
4    0.549906
5   -0.479970
6   -1.991117
7   -0.407330
8   -0.131100
9    0.388155
dtype: float64
type(s)
pandas.core.series.Series
len(s)
10
s.shape
(10,)
for x in s: 
    print(x)
0.2523733143388311
-1.0753068117512616
-0.36870536789029595
0.7845995696103139
0.5499055415241653
-0.47997013360871355
-1.9911174129623606
-0.40733045301228693
-0.13110029817116495
0.38815521816133036

4. Pandas: 행과 열의 선택

- 두 가지 형태의 데이터프레임

dct = {'date': ['12/30','12/31','01/01','01/02','01/03'], 'X1': [65,95,65,55,80], 'X2': [55,100,90,80,30], 'X3': [50,50,60,75,30], 'X4': [40,80,30,80,100]}
df = pd.DataFrame(dct) 
df
date X1 X2 X3 X4
0 12/30 65 55 50 40
1 12/31 95 100 50 80
2 01/01 65 90 60 30
3 01/02 55 80 75 80
4 01/03 80 30 30 100
ts = pd.DataFrame({'X1': [65,95,65,55,80], 'X2': [55,100,90,80,30], 'X3': [50,50,60,75,30], 'X4': [40,80,30,80,100]}, index=['12/30','12/31','01/01','01/02','01/03']) 
ts
X1 X2 X3 X4
12/30 65 55 50 40
12/31 95 100 50 80
01/01 65 90 60 30
01/02 55 80 75 80
01/03 80 30 30 100

A. 열의 선택

df
date X1 X2 X3 X4
0 12/30 65 55 50 40
1 12/31 95 100 50 80
2 01/01 65 90 60 30
3 01/02 55 80 75 80
4 01/03 80 30 30 100

- 방법1: df.? + col-name

# df.X1

- 방법2: df[?] + str, [str,str]

# df['X1'] # str 
# df[['X1']] # [str]
# df[['X1','X3']] # [str,str]

- 방법3: df.iloc[:,?] + int, int:int, [int,int], [bool,bool], range

# df.iloc[:,0] # int
# df.iloc[:,-2:] # int:int - 슬라이싱
# df.iloc[:,1::2] # int:int - 스트라이딩
# df.iloc[:,[0]] # [int]
# df.iloc[:,[0,1]] # [int,int]
# df.iloc[:,[True,True,False,False]] # bool의 list 
# df.iloc[:,range(2)] # range

- 방법4: df.loc[:,?] + str, ‘str:str’, [str,str], [bool,bool]

# df.loc[:,'X1'] # str
# df.loc[:,'X1':'X3'] # 'str':'str' -- 칼럼이름으로 슬라이싱 **
# df.loc[:,'X1'::2] # 'str':'str' -- 칼럼이름으로 스트라이딩 ** 
# df.loc[:,['X1']] # [str]
# df.loc[:,['X1','X4']] # [str,str]
# df.loc[:,[True,False,False,True]] # bool의 list

B. 행의 선택

- 방법1: df[] + int:int, str:str, [bool,bool], pd.Series([bool,bool]) – \((\star\star\star\star\star)\)

# df[:2] # int:int -- 슬라이싱 // df.iloc[:2,:], df.iloc[:2] 와 같음
# df[::2] # int:int -- 스트라이딩 
# ts['12/30':'01/02'] # str:str -- 슬라이싱
# ts['12/31'::2] # str:str -- 스트라이딩
# df[['12' in date for date in df.date]] # [bool,bool]
# df[df.X1 < 70] # pd.Series([bool,bool])

- 방법2: df.iloc[], df.iloc[,:] + int, int:int, [int,int], [bool,bool], range

# df.iloc[0] # int 
# df.iloc[-2:] # int:int -- 슬라이싱
# df.iloc[1::2] # int:int -- 스트라이딩
# df.iloc[[0]] # [int]
# df.iloc[[0,1]] # [int,int]
# df.iloc[['12' in date for date in df.date]] # [bool,bool]
# df.iloc[range(2)] # range
# df.iloc[0,:] # int 
# df.iloc[-2:,:] # int:int -- 슬라이싱
# df.iloc[1::2,:] # int:int -- 스트라이딩
# df.iloc[[0],:] # [int]
# df.iloc[[0,1],:] # [int,int]
# df.iloc[['12' in date for date in df.date],:] # [bool,bool]
# df.iloc[range(2),:] # range

- 방법3: df.loc[], df.loc[,:] + int, str, int:int, str:str, [int,int], [str,str], [bool,bool], pd.Series([bool,bool])

# df.loc[0] # int 
# ts.loc['12/30'] # str 
# df.loc[:2] # int:int 
# ts.loc[:'01/02'] # str:str 
# df.loc[[0,1]] # [int,int]
# ts.loc[['12/30','01/01']] # [str,str]
# df.loc[['12' in date for date in df.date]] # [bool,bool]
# df.loc[df.X1>70] # pd.Series([bool,bool]) 

C. 제 스타일

- 가장 안전한 코드

# df.loc[:,:]

- 상황1: 하나의 col을 뽑으려 할때 좋은 코드

# df.X1 # 최애 
# df['X1'] # 차애 
# df[['X1']] # 차애

- 상황2: row 슬라이싱을 할때 좋은 코드 \((\star\star\star)\)

# df[:5] # 최애 
# ts[:'01/02'] # 시계열인 경우 

- 상황3: 조건에 맞는 row를 뽑을때 좋은 코드

# df[df.X1<60] # 최애
# df.loc[['12' in date for date in df.date]] # 차애

- 상황4: 하나의 row를 뽑으려 할때 좋은 코드

# df.iloc[0] # 최애 
# df.loc[0] # 차애

- 상황5: (row,col)을 뽑으려 할때 좋은 코드

# 최애: pd.Series를 뽑고 -> 인덱스로접근
# df.X1[0]
# df['X1'][0]

# 차애: iloc, loc 으로 한번에 뽑기
# df.iloc[0,0]
# df.loc[0,'X1']

위의 상황이외에는 df.loc[:,:]를 사용하는것이 유리하다

- 상황6: column 슬라이싱을 할때

# df.loc[:,'X1':'X3'] # 끝점포함

- 상황7: row + column 슬라이싱을 하는 가장 좋은 코드

# df.loc[::2,'X1':'X2']

- 상황8: 조건에 맞는 col을 뽑기에 가장 좋은 코드

# df.loc[:,[len(colname)>2 for colname in df.columns]]

- 상황9: 조건에 맞는 row, col을 뽑기에 가장 좋은 코드

# df.loc[df.X1>70,[len(colname)>2 for colname in df.columns]]

D. 제 스타일 X

- 제가 안쓰는 코드1

df[:1]
date X1 X2 X3 X4
0 12/30 65 55 50 40

이러면 내 입장에서는 마치 아래가 동작할 것 같잖아..

df[0] 
KeyError: 0

- 제가 안쓰는 코드2: bool의 list를 사용할때 iloc은 가급적 쓰지마세요

df.iloc[list(df['X1']<80),:]
date X1 X2 X3 X4
0 12/30 65 55 50 40
2 01/01 65 90 60 30
3 01/02 55 80 75 80

이러면 마치 아래도 동작할 것 같잖아..

df.iloc[df['X1']<80,:]
NotImplementedError: iLocation based boolean indexing on an integer type is not available

E. 요약

- 알아두면 좋은 규칙

  • .iloc[].iloc[,:]는 완전히 동등하다.
  • .loc[].loc[,:]는 완전히 동등하다.
  • 결과를 pd.Series 형태가 아닌 pd.DataFrame 형태로 얻고 싶다면 [[?]]를 사용하면 된다.

- 정리

type of indexer . [] .iloc .loc 내가 쓴다면?
int X X O \(\Delta\) df.iloc[3,:]
int:int X O O \(\Delta\) df[3:5]
[int,int] X X O \(\Delta\) df.iloc[idx,:]
str X X X O ts.loc['time1',:]
str:str X O X O ts.loc['time1':'time2',:]
[str,str] X X X O 안할 듯
[bool,bool] X O O O df[filtered_idx]
pd.Series([bool,bool]) X O X O df[df.X1>20]
type of indexer . [] .iloc .loc 내가 쓴다면?
int X X O X df.iloc[:,0]
int:int X X O X df.iloc[:,0:2]
[int,int] X X O X df.iloc[:,idx]
str O O X O df.loc[:,'X1']
str:str X X X O df.loc[:,'X1':'X4']
[str,str] X O X O df.loc[:,colname_list]
[bool,bool] X X O O df.loc[:,bool_list]

5. Pandas: 기본기능

A. 열의 이름 변경

- 방법1: df.columns에 대입

df = pd.DataFrame(np.random.randn(3,2))
df.columns = ['A','B']
df
A B
0 0.037335 -0.540520
1 -0.019184 -0.711376
2 0.320825 -0.245878

- 방법2: df.set_axis()

df = pd.DataFrame(np.random.randn(3,2))
df.set_axis(['A','B'],axis=1)
A B
0 -0.252874 1.569584
1 -0.957351 -0.120713
2 1.400832 -0.916344

- 방법3: df.rename()

df = pd.DataFrame(np.random.randn(3,2))
df.rename({0:'AA',1:'BB'},axis=1)
AA BB
0 -1.793611 0.047263
1 -0.326602 0.054525
2 -1.117781 0.405474

B. 행의 이름 변경

- 방법1: df.index에 대입

df = pd.DataFrame(np.random.randn(3,2))
df
0 1
0 -1.074150 0.030191
1 -2.527365 0.964030
2 0.305062 -0.858343
df.index = ['guebin','jiyoon','boram']
df
0 1
guebin -1.074150 0.030191
jiyoon -2.527365 0.964030
boram 0.305062 -0.858343

- 방법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() 이용
df.rename({l:l.replace(' ','_') for l in df.columns if ' ' in l},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()
df.rename({i:j for i,j in zip(df.index, df.ID)})
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[:,: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[:,:2])
pd.reset_option("display.max_rows")
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
... ... ...
Height 189cm 179cm
Weight 82kg 69kg
Release Clause €157M €155M
Kit Number 8.0 8.0
Best Overall Rating NaN NaN

29 rows × 2 columns

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

D. df.dtypes, s.dtype, df.select_dtypes()

- 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 == object
df.Name.dtype == np.object_
True
df.Age.dtype == int
df.Age.dtype == np.int64
True
df['International Reputation'].dtype == float
df['International Reputation'].dtype == np.float64
True

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

- (풀이1)

df.loc[:,[l == int for l in df.dtypes]]
df.loc[:,[l == np.int64 for l 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

- (풀이2)

df.select_dtypes(int)
df.select_dtypes(np.int64)
df.select_dtypes([int])
df.select_dtypes([np.int64])
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에서 int, float 자료형만 출력 – select_dtypes() 이용

df.select_dtypes([int,float])
df.select_dtypes([np.int64,np.float64])
df.select_dtypes('number')
df.select_dtypes(['number'])
ID Age Overall Potential Special International Reputation Weak Foot Skill Moves Kit Number
0 209658 27 87 88 2312 4.0 4.0 3.0 8.0
1 212198 27 86 87 2305 3.0 3.0 4.0 8.0
2 224334 30 85 85 2303 2.0 3.0 3.0 19.0
3 192985 31 91 91 2303 4.0 5.0 4.0 17.0
4 224232 25 86 89 2296 3.0 3.0 3.0 23.0
... ... ... ... ... ... ... ... ... ...
17655 269526 19 48 61 762 1.0 3.0 1.0 35.0
17656 267946 17 48 64 761 1.0 2.0 1.0 21.0
17657 270567 25 51 56 759 1.0 2.0 1.0 12.0
17658 256624 18 50 65 758 1.0 2.0 1.0 40.0
17659 256376 20 50 61 749 1.0 2.0 1.0 30.0

17660 rows × 9 columns

#

# 예제3: df에서 object 자료형만 출력 – select_dtypes() 이용

df.select_dtypes(object)
df.select_dtypes([object])
Name Photo Nationality Flag Club Club Logo Value Wage Preferred Foot Work Rate Body Type Real Face Position Joined Loaned From Contract Valid Until Height Weight Release Clause Best Overall Rating
0 L. Goretzka https://cdn.sofifa.net/players/209/658/23_60.png Germany https://cdn.sofifa.net/flags/de.png FC Bayern München https://cdn.sofifa.net/teams/21/30.png €91M €115K Right High/ Medium Unique Yes <span class="pos pos28">SUB Jul 1, 2018 NaN 2026 189cm 82kg €157M NaN
1 Bruno Fernandes https://cdn.sofifa.net/players/212/198/23_60.png Portugal https://cdn.sofifa.net/flags/pt.png Manchester United https://cdn.sofifa.net/teams/11/30.png €78.5M €190K Right High/ High Unique Yes <span class="pos pos15">LCM Jan 30, 2020 NaN 2026 179cm 69kg €155M NaN
2 M. Acuña https://cdn.sofifa.net/players/224/334/23_60.png Argentina https://cdn.sofifa.net/flags/ar.png Sevilla FC https://cdn.sofifa.net/teams/481/30.png €46.5M €46K Left High/ High Stocky (170-185) No <span class="pos pos7">LB Sep 14, 2020 NaN 2024 172cm 69kg €97.7M NaN
3 K. De Bruyne https://cdn.sofifa.net/players/192/985/23_60.png Belgium https://cdn.sofifa.net/flags/be.png Manchester City https://cdn.sofifa.net/teams/10/30.png €107.5M €350K Right High/ High Unique Yes <span class="pos pos13">RCM Aug 30, 2015 NaN 2025 181cm 70kg €198.9M NaN
4 N. Barella https://cdn.sofifa.net/players/224/232/23_60.png Italy https://cdn.sofifa.net/flags/it.png Inter https://cdn.sofifa.net/teams/44/30.png €89.5M €110K Right High/ High Normal (170-) Yes <span class="pos pos13">RCM Sep 1, 2020 NaN 2026 172cm 68kg €154.4M NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17655 Deng Xiongtao https://cdn.sofifa.net/players/269/526/23_60.png China PR https://cdn.sofifa.net/flags/cn.png Meizhou Hakka https://cdn.sofifa.net/teams/114628/30.png €100K €500 Right Medium/ Medium Normal (185+) No <span class="pos pos29">RES Apr 11, 2022 NaN 2027 190cm 78kg €218K NaN
17656 22 Lim Jun Sub https://cdn.sofifa.net/players/267/946/22_60.png Korea Republic https://cdn.sofifa.net/flags/kr.png Jeju United FC https://cdn.sofifa.net/teams/1478/30.png €100K €500 Right Medium/ Medium Lean (185+) No <span class="pos pos29">RES Jan 1, 2022 NaN 2026 195cm 84kg €188K NaN
17657 A. Demir https://cdn.sofifa.net/players/270/567/23_60.png Turkey https://cdn.sofifa.net/flags/tr.png Ümraniyespor https://cdn.sofifa.net/teams/113796/30.png €70K €2K Right Medium/ Medium Lean (185+) No <span class="pos pos29">RES Jun 6, 2021 NaN 2023 190cm 82kg €142K NaN
17658 21 S. Czajor https://cdn.sofifa.net/players/256/624/21_60.png Poland https://cdn.sofifa.net/flags/pl.png Fleetwood Town https://cdn.sofifa.net/teams/112260/30.png €90K €500 Right Medium/ Medium Normal (185+) No <span class="pos pos29">RES Jan 1, 2020 NaN 2021 187cm 79kg €214K NaN
17659 21 F. Jakobsson https://cdn.sofifa.net/players/256/376/21_60.png Sweden https://cdn.sofifa.net/flags/se.png IFK Norrköping https://cdn.sofifa.net/teams/702/30.png €90K €500 Left Medium/ Medium Normal (185+) No <span class="pos pos29">RES Jan 8, 2020 NaN 2021 186cm 78kg €131K NaN

17660 rows × 20 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

6. Pandas: query

ts = pd.DataFrame(np.random.normal(size=(20,4)),columns=list('ABCD'),index=pd.date_range('20221226',periods=20)).assign(Sex=['M']*10+['F']*10)
ts
A B C D Sex
2022-12-26 0.913373 1.273686 -1.031498 0.107483 M
2022-12-27 -0.231464 -2.486921 -2.037792 0.455930 M
2022-12-28 0.044797 0.013809 -0.780978 0.288263 M
2022-12-29 0.584282 1.763700 0.943253 0.153924 M
2022-12-30 0.210974 -0.691147 -0.484299 0.265967 M
2022-12-31 1.062767 -0.958027 -0.128911 -0.156103 M
2023-01-01 1.358383 0.972339 -0.888977 -1.260350 M
2023-01-02 -0.095874 -0.382660 0.846097 0.490706 M
2023-01-03 -0.918114 0.040202 -0.837528 0.290037 M
2023-01-04 -1.651541 1.264967 0.334392 -1.445567 M
2023-01-05 -0.164268 -0.424999 1.506550 -0.254232 F
2023-01-06 -0.949612 1.078584 0.079759 0.897018 F
2023-01-07 -0.795211 0.749976 1.882239 -0.847576 F
2023-01-08 -0.326690 -0.265898 0.398331 0.198163 F
2023-01-09 -0.770815 -0.143819 0.095757 -0.716950 F
2023-01-10 -0.519762 0.619891 0.317133 -0.728839 F
2023-01-11 1.033974 -0.098040 -1.367873 -0.777833 F
2023-01-12 0.351120 0.137920 -1.809062 0.470261 F
2023-01-13 -0.789317 -0.179441 0.615646 -0.110530 F
2023-01-14 0.183356 0.362680 0.318493 -1.005962 F

A. 기본 query

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

#ts[(ts.A>0) & (ts.B<0)]
ts.query('A>0 and B<0')
A B C D Sex
2022-12-30 0.210974 -0.691147 -0.484299 0.265967 M
2022-12-31 1.062767 -0.958027 -0.128911 -0.156103 M
2023-01-11 1.033974 -0.098040 -1.367873 -0.777833 F

- 예시2: A<B<C

ts.query('A<B<C')
A B C D Sex
2023-01-07 -0.795211 0.749976 1.882239 -0.847576 F
2023-01-08 -0.326690 -0.265898 0.398331 0.198163 F
2023-01-09 -0.770815 -0.143819 0.095757 -0.716950 F
2023-01-13 -0.789317 -0.179441 0.615646 -0.110530 F

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

#ts[(ts.A + ts.B)/2 >0]
ts.query('(A+B)/2 >0')
A B C D Sex
2022-12-26 0.913373 1.273686 -1.031498 0.107483 M
2022-12-28 0.044797 0.013809 -0.780978 0.288263 M
2022-12-29 0.584282 1.763700 0.943253 0.153924 M
2022-12-31 1.062767 -0.958027 -0.128911 -0.156103 M
2023-01-01 1.358383 0.972339 -0.888977 -1.260350 M
2023-01-06 -0.949612 1.078584 0.079759 0.897018 F
2023-01-10 -0.519762 0.619891 0.317133 -0.728839 F
2023-01-11 1.033974 -0.098040 -1.367873 -0.777833 F
2023-01-12 0.351120 0.137920 -1.809062 0.470261 F
2023-01-14 0.183356 0.362680 0.318493 -1.005962 F

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

ts.query('(A+B)/2>0 and Sex=="M"')
A B C D Sex
2022-12-26 0.913373 1.273686 -1.031498 0.107483 M
2022-12-28 0.044797 0.013809 -0.780978 0.288263 M
2022-12-29 0.584282 1.763700 0.943253 0.153924 M
2022-12-31 1.062767 -0.958027 -0.128911 -0.156103 M
2023-01-01 1.358383 0.972339 -0.888977 -1.260350 M
ts.query("(A+B)/2>0 and Sex=='F'")
A B C D Sex
2023-01-06 -0.949612 1.078584 0.079759 0.897018 F
2023-01-10 -0.519762 0.619891 0.317133 -0.728839 F
2023-01-11 1.033974 -0.098040 -1.367873 -0.777833 F
2023-01-12 0.351120 0.137920 -1.809062 0.470261 F
2023-01-14 0.183356 0.362680 0.318493 -1.005962 F

B. 외부변수를 이용

- 예시: A > mean(A)

(풀이1)

ts[ts.A > -0.0734821041290727]
ts.query('A > A.mean()')
A B C D Sex
2022-12-26 0.913373 1.273686 -1.031498 0.107483 M
2022-12-28 0.044797 0.013809 -0.780978 0.288263 M
2022-12-29 0.584282 1.763700 0.943253 0.153924 M
2022-12-30 0.210974 -0.691147 -0.484299 0.265967 M
2022-12-31 1.062767 -0.958027 -0.128911 -0.156103 M
2023-01-01 1.358383 0.972339 -0.888977 -1.260350 M
2023-01-11 1.033974 -0.098040 -1.367873 -0.777833 F
2023-01-12 0.351120 0.137920 -1.809062 0.470261 F
2023-01-14 0.183356 0.362680 0.318493 -1.005962 F

(풀이2)

m = ts.A.mean()
ts[ts.A>m]
ts.query('A > @m')
A B C D Sex
2022-12-26 0.913373 1.273686 -1.031498 0.107483 M
2022-12-28 0.044797 0.013809 -0.780978 0.288263 M
2022-12-29 0.584282 1.763700 0.943253 0.153924 M
2022-12-30 0.210974 -0.691147 -0.484299 0.265967 M
2022-12-31 1.062767 -0.958027 -0.128911 -0.156103 M
2023-01-01 1.358383 0.972339 -0.888977 -1.260350 M
2023-01-11 1.033974 -0.098040 -1.367873 -0.777833 F
2023-01-12 0.351120 0.137920 -1.809062 0.470261 F
2023-01-14 0.183356 0.362680 0.318493 -1.005962 F

C. Index로 query

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

ts.query('index < "2022-12-30" or index == "2023-01-10"')
/tmp/ipykernel_642677/2579726232.py:1: FutureWarning: The behavior of 'isin' with dtype=datetime64[ns] and castable values (e.g. strings) is deprecated. In a future version, these will not be considered matching by isin. Explicitly cast to the appropriate dtype before calling isin instead.
  ts.query('index < "2022-12-30" or index == "2023-01-10"')
A B C D Sex
2022-12-26 0.913373 1.273686 -1.031498 0.107483 M
2022-12-27 -0.231464 -2.486921 -2.037792 0.455930 M
2022-12-28 0.044797 0.013809 -0.780978 0.288263 M
2022-12-29 0.584282 1.763700 0.943253 0.153924 M
2023-01-10 -0.519762 0.619891 0.317133 -0.728839 F

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').head(5)
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

5 rows × 29 columns

7. 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를 영구적으로 변화시킴

8. Pandas: transform column

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

df = pd.read_csv('https://raw.githubusercontent.com/guebin/DV2022/master/posts/FIFA23_official_data.csv').drop(['Loaned From','Best Overall Rating'],axis=1).dropna().reset_index(drop=True)
df.head()
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

5 rows × 27 columns

- (풀이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 ... 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 189 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 179 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 172 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 181 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 172 68kg €154.4M 23.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16359 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 190 78kg €218K 35.0
16360 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 195 84kg €188K 21.0
16361 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 190 82kg €142K 12.0
16362 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 187 79kg €214K 40.0
16363 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 186 78kg €131K 30.0

16364 rows × 27 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 ... 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 189 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 179 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 172 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 181 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 172 68kg €154.4M 23.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16359 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 190 78kg €218K 35.0
16360 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 195 84kg €188K 21.0
16361 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 190 82kg €142K 12.0
16362 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 187 79kg €214K 40.0
16363 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 186 78kg €131K 30.0

16364 rows × 27 columns

#

# 예시2 df.Joined을 이용하여 선수들이 join한 연도를 알아내고 이를 새로운 열로 할당하라.

- (풀이1)

df.assign(JoinedYear = list(map(lambda x: int(x[-4:]), df.Joined)))
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Body Type Real Face Position Joined Contract Valid Until Height Weight Release Clause Kit Number JoinedYear
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 ... Unique Yes <span class="pos pos28">SUB Jul 1, 2018 2026 189cm 82kg €157M 8.0 2018
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 ... Unique Yes <span class="pos pos15">LCM Jan 30, 2020 2026 179cm 69kg €155M 8.0 2020
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 ... Stocky (170-185) No <span class="pos pos7">LB Sep 14, 2020 2024 172cm 69kg €97.7M 19.0 2020
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 ... Unique Yes <span class="pos pos13">RCM Aug 30, 2015 2025 181cm 70kg €198.9M 17.0 2015
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 ... Normal (170-) Yes <span class="pos pos13">RCM Sep 1, 2020 2026 172cm 68kg €154.4M 23.0 2020
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16359 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 ... Normal (185+) No <span class="pos pos29">RES Apr 11, 2022 2027 190cm 78kg €218K 35.0 2022
16360 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 ... Lean (185+) No <span class="pos pos29">RES Jan 1, 2022 2026 195cm 84kg €188K 21.0 2022
16361 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 ... Lean (185+) No <span class="pos pos29">RES Jun 6, 2021 2023 190cm 82kg €142K 12.0 2021
16362 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 ... Normal (185+) No <span class="pos pos29">RES Jan 1, 2020 2021 187cm 79kg €214K 40.0 2020
16363 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 ... Normal (185+) No <span class="pos pos29">RES Jan 8, 2020 2021 186cm 78kg €131K 30.0 2020

16364 rows × 28 columns

- (풀이2)

df.assign(JoinedYear = [int(l[-4:]) for l in df.Joined])
ID Name Age Photo Nationality Flag Overall Potential Club Club Logo ... Body Type Real Face Position Joined Contract Valid Until Height Weight Release Clause Kit Number JoinedYear
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 ... Unique Yes <span class="pos pos28">SUB Jul 1, 2018 2026 189cm 82kg €157M 8.0 2018
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 ... Unique Yes <span class="pos pos15">LCM Jan 30, 2020 2026 179cm 69kg €155M 8.0 2020
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 ... Stocky (170-185) No <span class="pos pos7">LB Sep 14, 2020 2024 172cm 69kg €97.7M 19.0 2020
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 ... Unique Yes <span class="pos pos13">RCM Aug 30, 2015 2025 181cm 70kg €198.9M 17.0 2015
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 ... Normal (170-) Yes <span class="pos pos13">RCM Sep 1, 2020 2026 172cm 68kg €154.4M 23.0 2020
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16359 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 ... Normal (185+) No <span class="pos pos29">RES Apr 11, 2022 2027 190cm 78kg €218K 35.0 2022
16360 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 ... Lean (185+) No <span class="pos pos29">RES Jan 1, 2022 2026 195cm 84kg €188K 21.0 2022
16361 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 ... Lean (185+) No <span class="pos pos29">RES Jun 6, 2021 2023 190cm 82kg €142K 12.0 2021
16362 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 ... Normal (185+) No <span class="pos pos29">RES Jan 1, 2020 2021 187cm 79kg €214K 40.0 2020
16363 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 ... Normal (185+) No <span class="pos pos29">RES Jan 8, 2020 2021 186cm 78kg €131K 30.0 2020

16364 rows × 28 columns

9. 반복문 – 자투리

A. zip

- 예시1

x = [11,22,33]
y = [44,55,66]
for xi,yi in zip(x,y):
    print(xi,yi)
11 44
22 55
33 66

- 예시2

df = pd.DataFrame({'year':[2022,2023,2022,2022,2019],'month':[12,10,8,2,1],'day':[31,22,31,28,15]})
df
year month day
0 2022 12 31
1 2023 10 22
2 2022 8 31
3 2022 2 28
4 2019 1 15
df.assign(ymd = [f"{y}-{m}-{d}" for y,m,d in zip(df.year, df.month, df.day)])
year month day ymd
0 2022 12 31 2022-12-31
1 2023 10 22 2023-10-22
2 2022 8 31 2022-8-31
3 2022 2 28 2022-2-28
4 2019 1 15 2019-1-15

B. enumerate

- 예시1

x = [11,22,33]
for i,xi in enumerate(x):
    print(i,xi)
0 11
1 22
2 33

- 예시2

lst = np.random.choice(['사과','딸기','배'],100,replace=True).tolist()
lst[:5]
['배', '사과', '사과', '딸기', '딸기']
dct = {l:i for i,l in enumerate(set(lst))}
dct
{'딸기': 0, '사과': 1, '배': 2}
[dct[l] for l in lst][:5]
[2, 1, 1, 0, 0]

10. 변수의 범위

- 모티브: 주피터노트북 하나를 키고 x에 100을 넣는다. 다른 주피터를 하나 키고 x를 치면 100이 나올까? –> “독립된 환경”이라는 개념

A. 함수

- 예시1: 함수내부 / 함수외부

x = None 
y = None 
def f(x):
    y = x+1
    return y 
f(1)
2
x,y
(None, None)

여기에서 x,y는 함수내부에서만 선언 및 계산되고 소멸한다. 즉 함수안에서 사용되는 코드는 마치 독립된 환경처럼 작동한다. 함수외부의 x,y는 함수내부의 x,y와 “기본적으로는” 상관이 없다.

- 예시2: 함수내부 / 함수외부

x = 1
y = None 
def f(x):
    y = x+1
    return y 
f(x)
2
x,y
(1, None)

여기에서 x는 함수외부에 선언되었고, 함수 외부에 선언된 x의 값이 함수 내부로 전달되었다. 전달된 값은 함수내부에서 x라는 이름으로 저장되었음.

- 예시3: 함수내부 / 함수외부

x = None 
y = None 
def f(x):
    y = x+1
    return y 
z = 1
f(z)
2
x,y
(None, None)

여기에서 z는 함수외부에 선언되었고, 함수 외부에 선언된 z의 값이 함수 내부로 전달되었음. 전달된 값은 함수내부에서 x라는 이름으로 저장되었음.

- 예시4: 함수내부 / 함수외부

x = 2
y = 3
def f(x):
    return x+y # 함수내부에서는 y를 정의한적이 없잖아? (뭘 x에 더하라는거야?) 혹시 함수 외부에 y가 정의되어있나? 찾아보니 y=3이있음. 이걸 가져다가 쓰자 
f(10)
13
x,y
(2, 3)

여기에서 x=2,y=3는 함수외부에 선언되었음. 함수 내부에는 x=10 이 선언되었음. 함수내부에서 x+y를 계산하려고 했는데, y는 함수내부에 없음. 그런데 함수외부에는 y가 있네? 그럼 함수외부에 있는 y를 빌려옴!! (이건 엄청 위험한 코드임. 왜냐하면 y가 어디있는지 쉽게 찾을 수 없기 때문!)

사실 이 코드에서 y와 같은 역할을 하는 변수를 “전역변수”라고 하는데, y가 전역변수를 의미할때는 보통 대문자를 사용한다.

- 예시5: 전역변수를 사용하는 모범코드

PI = 3.141592
def f(r):
    return PI*(r**2)
def g(r): 
    return (4/3)*PI*(r**3)
f(2),g(2)
(12.566368, 33.510314666666666)

- 예시6: 전역변수와 지역변수가 충돌한다면?

x = 5
y = 5
def f(x):
    y = x+1 
    return x**y
f(2)
8
x,y
(5, 5)

이거 사실 예시1이랑 같은코드에요, 전역변수 개념이 너무 충격적이라서 갑자기 헷갈릴 뿐입니다.

B. for / 컴프리헨션

- 예시1: for문과 함께 사용된 \(i\)

i = None
lst = None 
i, lst 
(None, None)
lst = []
for i in range(5):
    lst.append(i)
i, lst
(4, [0, 1, 2, 3, 4])

- 예시2: 컴프리헨션과 사용된 \(i\)

i = None
lst = None 
lst = [i for i in range(5)]
i, lst
(None, [0, 1, 2, 3, 4])

A1. 잔소리

A. 통계학과의 진로

- 코딩과 상관 X

  • 시험을 통해 얻는 직업: 회계사, 공무원/공기업, …
  • 본인이 선택하는 직업: 가업, 학원선생님, 전과 이후 새 직업 탐색, … 1
  • 1 사실상 졸업을 위해서 수강하는 학생들

  • - 코딩과 상관 O

    • 당장 코딩실력이 필요할 경우 (= 코딩테스트를 준비해야 하는 경우): IT기업 (네이버, 카카오, …), 빅데이터분석기업(LG-CNS, 삼성SDS, …), 기타대기업(삼성전자, 하이닉스, SKT, KT, …),
    • 당장 코딩실력이 필요하진 않지만 장기적으론 필요한 경우: 대학원진학, 금융권 (XX은행, XX증권, …), 병원(전북대병원등,…)

    - 시험을 통해 얻는 직업 vs 일반사기업입사 (혹은 대학원)

    • 사법고시공부 vs 좋은대학 느낌
    • 할 수 있다면 시험준비해서 좋은기업가면 좋음.
    • 그렇지 않다면 “공모전준비(캐글/데이콘경진대회경험등)”+“학점관리”+“영어공부”+“코딩테스트준비”+“학회활동”+“자격증대비” -> 대학원 or 취업

    - 이 수업의 목적:

    1. 통계학과 상위과목 수강을 위한 실력쌓기: 기계학습활용, 기계학습, 딥러닝, 데이터마이닝 등.. 파이썬을 활용하는 모든 과목들

    2. 코딩 흡수력 향상: 남의 코드를 보고 흡수할 수 있느냐?

    'A'.isupper()
    True
    'A'.islower()
    False

    3. 코딩테스트에 대한 대비 X (스트레스만 줄 것임)

    B. 코딩테스트를 잘하면 좋은것

    - 삼성 청년 SW 아카데미 https://www.ssafy.com/ksp/jsp/swp/apply/swpApplyProcess.jsp

    • 전문화된 SW교육 제공
    • 교육지원금 지급 (매월 100만원)
    • 국내외 연구소 실습기회 부여
    • 이 자체가 엄청난 스펙..

    - 카카오 2024 채용연계형 겨울 인턴십 모집 https://www.kakaocorp.com/page/detail/10677

    • 인턴십 이후 전환 인터뷰를 통해 최종 정규직 전환 대상자가 확정
    • 전환 인터뷰에 통과하면 내년 3월부터 카카오 크루로 입사
    • 단, 학업 등의 이유로 부득이하게 내년 3월 입사가 어려운 경우 2024년 9월 이내 가능한 일자로 별도 조율할 예정
    • 학생들 입장에서는 유일한 입사기회 + 떨어져도 좋은 경험
    • 최근에 이런 형태로 뽑는 기업들 많음

    - 삼성 SW 역량테스트 https://swexpertacademy.com/main/capacityTest/main.do

    - 유용한사이트

    - 학생들의 노력

    C. 코딩테스트 잘보는 방법?

    - 코딩테스트문제 = 구현문제 + 알고리즘문제

    • 알고리즘문제는 따로 공부해야함. (풀이 패턴이 있음, 퀴즈5-1~2)
    • 구현문제는 평소에 논리적 생각을 많이해야함 + 꼼꼼해야함 + 오류수정잘해야함(중간고사3)

    - 언어특화문법을 잘 이해해야함

    • 파이썬스러운 코딩 (퀴즈5-1, 퀴즈5-3)

    - 사용가능한 패키지를 잘 확인할 것: numpy, pandas 활용가능 유무

    - 계산 시간을 따질 것