Chapter 11. Pandas I#
ํ์ต๋ชฉํ์ ๊ธฐ๋ํจ๊ณผ
ํ์ต๋ชฉํ
Pandas์ ์๋ฃ ๊ตฌ์กฐ์ธ Series์ DataFrame์ ๊ตฌ์กฐ์ ๋ํด ์์๋ณธ๋ค.
Series์ ์์ฑ, ์ฐ์ฐ, ์ธ๋ฑ์ฑ/์ฌ๋ผ์ด์ฑ ๋ฑ์ ์ค์ตํด๋ณธ๋ค.
DataFrame์ ์์ฑ, ์ฐ์ฐ, ์ธ๋ฑ์ฑ/์ฌ๋ผ์ด์ฑ ๋ฑ์ ์ค์ตํด๋ณธ๋ค.
๊ธฐ๋ํจ๊ณผ
Series์ DataFrame๋ฅผ ํ์ฉํ์ฌ ๋ค์ํ ๋ฐ์ดํฐ ์ฐ์ฐ ๋ฐ ๋ถ์์ ํ์ํ ๊ธฐ๋ฅ์ ์ฝ๊ณ ๋น ๋ฅด๊ฒ ํ ์ ์๋ค.
ํ๋ค์ค๋ ํ์ด์ฌ์์ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
โpandasโ๋ผ๋ ์ด๋ฆ์ ๋ค์ฐจ์์ผ๋ก ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ปํ๋ ๊ฒฝ์ ํ ์ฉ์ด์ธ Panel data์ ํ์ด์ฌ ๋ฐ์ดํฐ ๋ถ์์ธ Python data analysis์์ ๋ฐ์จ ๊ฒ์ด๋ค.
2008๋ ๊ธ์ต๋ฐ์ดํฐ ๋ถ์์ฉ์ผ๋ก ์ฒ์ ๊ฐ๋ฐ๋์๋ค.
๋์ฉ๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋๋ฐ ์ต์ ํ๋ ๋๊ตฌ์ด๋ฉฐ, ๋ฐ์ดํฐ์ ์ฌ๋ฐฐ์น, ์ง๊ณ, ๋ถ๋ถ์งํฉ ๊ตฌํ๊ธฐ ๋ฑ์ ์ฝ๊ฒ ํ ์ ์๋ค.
Pandas๋ Series์ DataFrame์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ ๊ณตํ๋ค.
์ถ์ฒ์๋ฃ:
https://wikidocs.net/book/7188 :[Python ์์ ์ ๋ณต ์๋ฆฌ์ฆ] 2ํธ : Pandas DataFrame ์์ ์ ๋ณต
https://pandas.pydata.org/docs/reference/index.html : Pandas ๊ณต์์ฌ์ดํธ ๋ฌธ์
Series#
์๋ฆฌ์ฆ๋ 1์ฐจ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ตฌ์กฐ์ด๋ค.
์ด๋ ๊ฐ์ ์ธ๋ฑ์ค ๋ฒํธ(์์น ์ธ๋ฑ์ค)๋ก๋ ์ ๊ทผ ํ ์ ์๊ณ ์ธ๋ฑ์ค๋ช (์ค์ ์ธ๋ฑ์ค)์ผ๋ก๋ ์ ๊ทผํ ์ ์๋ค.
์ฆ, ์ธ๋ฑ์ค ๋ฒํธ๋ก ๊ฐ์ ์ ๊ทผํ ์ ์๋ ๋ฆฌ์คํธ(list)์ ์ธ๋ฑ์ค๋ช ๊ณผ ๊ฐ์ ํค(key)๋ก ๊ฐ์ ์ ๊ทผํ ์ ์๋ ๋์ ๋๋ฆฌ(dict)์ ์ฅ์ ์ ์์ด ๋์ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
์๋ฆฌ์ฆ ์์ฑ#
์๋ฆฌ์ฆ๋ฅผ ์์ฑํ๊ธฐ ์ํด์๋ ๋จผ์ pandas ๋ชจ๋์ ๋ถ๋ฌ์์ผ ํ๋ค.
pandas ๋ชจ๋์ ๋ณดํต pd๋ผ๋ ๋ณ์นญ์ ์ฌ์ฉํ๋ค.
import pandas as pd
์๋ฆฌ์ฆ๋ฅผ ์์ฑํด๋ณด์. ํ์์ ์๋์ ๊ฐ๋ค. ์ด๋ Series์ โSโ๋ฅผ ๋๋ฌธ์๋ก ์จ์ผ ํ๋ค๋ ๊ฒ์ ์ฃผ์ํ์.
pd.Series(list or or dict or array, index = list or array)
์ธ๋ฑ์ค๋ฅผ ๋ณ๋ ์ค์ ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์์น ์ธ๋ฑ์ค๋ก ์ค์ ๋๋ค.
๋ง์ง๋ง์ ๋ฐ์ดํฐ ํ์ ์ด ํ์๋๋ค. dtype: int64๋ ์๋ฆฌ์ฆ ์ ๋ฐ์ดํฐ๊ฐ ์ ์ํ64bit ๋ฐ์ดํฐ์์ ์๋ฏธํ๋ค.
dtype |
์ค๋ช |
---|---|
|
์ ์ |
|
์์ |
|
ํ ์คํธ |
|
True/False |
|
๋ ์ง์ ์๊ฐ |
|
์นดํ ๊ณ ๋ฆฌ |
score = [84, 21, 87, 100, 59, 46]
s = pd.Series(score)
print(s)
0 84
1 21
2 87
3 100
4 59
5 46
dtype: int64
index ์ต์ ์ ํตํด ์ธ๋ฑ์ค๋ช ์ ์ค์ ํ ์ ์๋ค.
score = [84, 21, 87, 100, 59, 46]
names=['์ฒ ์','์์ด','๊ธธ๋','๋ฏธ์','์์ด','์ฒ ์ด']
s = pd.Series(score, index = names)
print(s)
์ฒ ์ 84
์์ด 21
๊ธธ๋ 87
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
dtype: int64
๋์ ๋๋ฆฌ๋ฅผ ๋ฃ์ด์ ์๋ฆฌ์ฆ๋ฅผ ์์ฑํ ์ ์๋ค.
dic={'์ฒ ์':84, '์์ด':21, '๊ธธ๋':87,'๋ฏธ์':100, '์์ด':59, '์ฒ ์ด':46}
s = pd.Series(dic)
print(s)
์ฒ ์ 84
์์ด 21
๊ธธ๋ 87
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
dtype: int64
type() ํจ์๋ก ์๋ฆฌ์ฆ์ ํ์ ์ ํ์ธํด๋ณด์.
print(type(s))
<class 'pandas.core.series.Series'>
์๋ฆฌ์ฆ์ ๋ชจ์์ ์๋ฆฌ์ฆ๋ช .shape์ผ๋ก ํ์ธํ ์ ์๋ค.
1์ฐจ์ ๋ฐฐ์ด์ (์์๊ฐฏ์, ) ํํ๋ก ๋์จ๋ค.
print(s.shape)
(6,)
์๋ฆฌ์ฆ ์ฐ์ฐ#
์๋ฆฌ์ฆ์ ์ฐ์ ์ฐ์ฐ์ ์ ์ฉํด๋ณด์. ์ด๋, ์ฐ์ฐํ์ฌ ๋์จ ๊ฒฐ๊ณผ ๋ํ ์๋ฆฌ์ฆํ์ ์ด๋ค.
import pandas as pd
names1=['์ฒ ์','์์ด','๊ธธ๋','๋ฏธ์','์์ด','์ฒ ์ด']
score1 = [84, 21, 87, 100, 59, 46]
names2 =['๊ธธ๋','์ฒ ์','์์ด','์ฒ ์ด','์์ด','๋ฏธ์']
score2 = [99, 87, 87, 84, 77, 15]
s1 = pd.Series(score1, index=names1)
s2 = pd.Series(score2, index=names2)
print(s1)
print(s2)
์ฒ ์ 84
์์ด 21
๊ธธ๋ 87
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
dtype: int64
๊ธธ๋ 99
์ฒ ์ 87
์์ด 87
์ฒ ์ด 84
์์ด 77
๋ฏธ์ 15
dtype: int64
s1 + 10
์ฒ ์ 94
์์ด 31
๊ธธ๋ 97
๋ฏธ์ 110
์์ด 69
์ฒ ์ด 56
dtype: int64
s1 + s2
๊ธธ๋ 186
๋ฏธ์ 115
์์ด 136
์์ด 108
์ฒ ์ 171
์ฒ ์ด 130
dtype: int64
s1 - s2
๊ธธ๋ -12
๋ฏธ์ 85
์์ด -18
์์ด -66
์ฒ ์ -3
์ฒ ์ด -38
dtype: int64
s1 * s2
๊ธธ๋ 8613
๋ฏธ์ 1500
์์ด 4543
์์ด 1827
์ฒ ์ 7308
์ฒ ์ด 3864
dtype: int64
(s1+s2)/2
๊ธธ๋ 93.0
๋ฏธ์ 57.5
์์ด 68.0
์์ด 54.0
์ฒ ์ 85.5
์ฒ ์ด 65.0
dtype: float64
s1%2
์ฒ ์ 0
์์ด 1
๊ธธ๋ 1
๋ฏธ์ 0
์์ด 1
์ฒ ์ด 0
dtype: int64
s1.sum()
397
์๋ฆฌ์ฆ ์ธ๋ฑ์ฑ#
์๋ฆฌ์ฆ์ ๋ฐ์ดํฐ์ ์ ๊ทผ์ ์ธ๋ฑ์ค ๋ฒํธ(์์น ์ธ๋ฑ์ค) ๋๋ ์ธ๋ฑ์ค๋ช (์ค์ ์ธ๋ฑ์ค)๋ฅผ ํตํด ๊ฐ๋ฅํ๋ค.
์๋ฆฌ์ฆ๋ช
[์์น์ธ๋ฑ์ค or ์ค์ ์ธ๋ฑ์ค]
s1[2]
87
s1['์์ด']
21
์๋ฆฌ์ฆ ์ฌ๋ผ์ด์ฑ#
์์น ์ธ๋ฑ์ค๋ก ์ฌ๋ผ์ด์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ฆฌ์คํธ๋ ๋ฐฐ์ด๊ณผ ๋์ผํ๋ค.
์ค์ ์ธ๋ฑ์ค๋ก ์ฌ๋ผ์ด์ฑํ๋ฉด
๋ ์ธ๋ฑ์ค๊น์ง ํฌํจ
ํ๋ค.
์๋ฆฌ์ฆ๋ณ์๋ช
[start:end:step]
s1[2:]
๊ธธ๋ 87
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
dtype: int64
๋ ์ธ๋ฑ์ค์ธ โ์์ดโ๊น์ง ํฌํจํ์ฌ ์ฌ๋ผ์ด์ฑํ๋ค.
s1['์์ด':'์์ด']
์์ด 21
๊ธธ๋ 87
๋ฏธ์ 100
์์ด 59
dtype: int64
s1['๋ฏธ์':]
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
dtype: int64
์๋ฆฌ์ฆ ๋ฐ์ดํฐ ์ถ๊ฐ, ์์ , ์ญ์ #
์๋ฆฌ์ฆ์ ๋ฐ์ดํฐ ์ถ๊ฐ์ ์์ ์ ํ์์ด ๋์ผํ๋ค. ์๋ฆฌ์ฆ์ ์ธ๋ฑ์ค๊ฐ ์์ผ๋ฉด ์์ ํ๊ณ , ์์ผ๋ฉด ์ถ๊ฐํ๋ค.
ํ์์ ๋ค์๊ณผ ๊ฐ๋ค.
์๋ฆฌ์ฆ๋ช
[์ธ๋ฑ์ค] = value
โ์ฌ๊ธฐโ๋ ์ธ๋ฑ์ค์ ์์ผ๋ฏ๋ก ์ถ๊ฐ๋๋ค.
s1['์ฌ๊ธฐ']= 87
print(s1)
์ฒ ์ 84
์์ด 21
๊ธธ๋ 87
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
์ฌ๊ธฐ 87
dtype: int64
โ๊ธธ๋โ์ ์ธ๋ฑ์ค์ ์์ผ๋ฏ๋ก ์์ ๋๋ค.
s1['๊ธธ๋']=88
print(s1)
์ฒ ์ 84
์์ด 21
๊ธธ๋ 88
๋ฏธ์ 100
์์ด 59
์ฒ ์ด 46
์ฌ๊ธฐ 87
dtype: int64
์๋ฆฌ์ฆ์์ ๋ฐ์ดํฐ ์ญ์ ๋ ํค์๋ del์ ํตํด ๊ฐ๋ฅํ๋ค.
์๋ ์ฝ๋๋ ์ธ๋ฑ์ค โ์ฒ ์ดโ๋ฅผ ์ญ์ ํ๋ค.
del s1['์ฒ ์ด']
print(s1)
์ฒ ์ 84
์์ด 21
๊ธธ๋ 88
๋ฏธ์ 100
์์ด 59
์ฌ๊ธฐ 87
dtype: int64
์๋ฆฌ์ฆ ๋น๊ต์ฐ์ฐ๊ณผ ํํฐ๋ง#
์๋ฆฌ์ฆ์์๋ ๋ํ์ด์์์ ๊ฐ์ด ๋น๊ต์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ธฐ๋ ์ฝ๋ค.
a = s1>=85
print(a)
์ฒ ์ False
์์ด False
๊ธธ๋ True
๋ฏธ์ True
์์ด False
์ฌ๊ธฐ True
dtype: bool
์๋ฆฌ์ฆ์์๋ ๋น๊ต์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํํฐ๋งํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
์๋ ์ฝ๋๋ ์๋ฆฌ์ฆ s1์์ 85์ด์์ธ ๊ฐ๋ค์ ๋ํด์ ํํฐ๋งํ๋ค.
s1[a]
๊ธธ๋ 88
๋ฏธ์ 100
์ฌ๊ธฐ 87
dtype: int64
DataFrame#
DataFrame์ ํ๊ณผ ์ด๋ก ์ด๋ฃจ์ด์ง 2์ฐจ์ ํํ์ ๋ฐฐ์ด์ด๋ค. ์ด๋ ํ ๊ฐ์ ์ด(์นผ๋ผ)์ ํ๋์ ์๋ฆฌ์ฆ์ด๋ฉฐ, DataFrame์ ์๋ฆฌ์ฆ๋ค์ ๋ฌถ์์ด๋ค.
๋ฐ์ดํํ๋ ์ ์์ฑ#
๋ฐ์ดํํ๋ ์ ์์ฑ ํ์์ ๋ค์๊ณผ ๊ฐ๋ค.
pd.DataFrame(list or array or series or dictionary, index = list or array, columns = list or array)
๋จผ์ , ์๋ฆฌ์ฆ๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐํ๋ ์์ ์์ฑํด๋ณด์.
์๋์ ๊ฐ์ด ์๋ฆฌ์ฆ 2๊ฐ๋ฅผ ๋ง๋ค์.
names1=['์ฒ ์','์์ด','๊ธธ๋','๋ฏธ์','์์ด','์ฒ ์ด']
score1 = [84, 21, 87, 100, 59, 46]
names2 =['๊ธธ๋','์ฒ ์','์์ด','์ฒ ์ด','์์ด','๋ฏธ์']
score2 = [99, 87, 87, 84, 77, 15]
s1 = pd.Series(score1, index=names1)
s2 = pd.Series(score2, index=names2)
์๋ฆฌ์ฆ๋ฅผ ๋ฐ์ดํํ๋ ์์ ์ด(column)๋ก ๋ฃ์ด์ค๋ค.
df = pd.DataFrame()
df['๊ตญ์ด']= s1 # ๊ตญ์ด ์นผ๋ผ ์์ฑ
df['์์ด']= s2 # ์์ด ์นผ๋ผ ์์ฑ
df['ํฉ๊ณ']=df['๊ตญ์ด']+df['์์ด'] # ํฉ๊ณ ์นผ๋ผ ์์ฑ
df
๊ตญ์ด | ์์ด | ํฉ๊ณ | |
---|---|---|---|
์ฒ ์ | 84 | 87 | 171 |
์์ด | 21 | 87 | 108 |
๊ธธ๋ | 87 | 99 | 186 |
๋ฏธ์ | 100 | 15 | 115 |
์์ด | 59 | 77 | 136 |
์ฒ ์ด | 46 | 84 | 130 |
์ธ๋ฑ์ฑ ์ฃผ์
์๋ฆฌ์ฆ์์ ์ธ๋ฑ์ฑ
์) s[โ์์ดโ]๋ก ํ๋ค๋ฉด โ์์ดโ๋ ํ์ ์ธ๋ฑ์ค์ด๋ค.
๋ฐ์ดํฐํ๋ ์ ์ธ๋ฑ์ฑ
์) df[โ๊ตญ์ดโ]๋ก ํ๋ค๋ฉด โ๊ตญ์ดโ๋ ์ด์ ์ธ๋ฑ์ค์ด๋ค.
๋ฐ์ดํํ๋ ์์ ๋ฆฌ์คํธ๋ ์ด๋ ์ด๋ก๋ ๋ง๋ค ์ ์๋ค.
scores = [[84,87,78], [21,15,84], [87,84,76], [100,87,99],[59,99,59],[46,77,56]]
d1 = pd.DataFrame(scores)
d1
0 | 1 | 2 | |
---|---|---|---|
0 | 84 | 87 | 78 |
1 | 21 | 15 | 84 |
2 | 87 | 84 | 76 |
3 | 100 | 87 | 99 |
4 | 59 | 99 | 59 |
5 | 46 | 77 | 56 |
๋ฐ์ดํฐํ๋ ์์ ์ต์ index์ columns์ ์ธ๋ฑ์ค์ ์นผ๋ผ๋ช ์ ์ง์ ๋ช ์ํด์ค ์๋ ์๋ค.
scores = [[84,87,78], [21,15,84], [87,84,76], [100,87,99],[59,99,59],[46,77,56]]
names=['์ฒ ์','์์ด','๊ธธ๋','๋ฏธ์','์์ด','์ฒ ์ด']
lectures=['๊ตญ์ด','์ํ','์์ด']
d2 = pd.DataFrame(scores, index=names, columns=lectures)
d2
๊ตญ์ด | ์ํ | ์์ด | |
---|---|---|---|
์ฒ ์ | 84 | 87 | 78 |
์์ด | 21 | 15 | 84 |
๊ธธ๋ | 87 | 84 | 76 |
๋ฏธ์ | 100 | 87 | 99 |
์์ด | 59 | 99 | 59 |
์ฒ ์ด | 46 | 77 | 56 |
๋์ ๋๋ฆฌ๋ฅผ ๋ฐ์ดํฐ๋ก ๋ฃ์ด ์ค ์๋ ์๋ค.
ScoresWithLectures={'๊ตญ์ด':[84,21,87,100,59,46], '์ํ':[87,15,84,87,99,77], '์์ด':[78,84,76,99,59,56]}
names=['์ฒ ์','์์ด','๊ธธ๋','๋ฏธ์','์์ด','์ฒ ์ด']
d3 = pd.DataFrame(ScoresWithLectures, index=names)
d3
๊ตญ์ด | ์ํ | ์์ด | |
---|---|---|---|
์ฒ ์ | 84 | 87 | 78 |
์์ด | 21 | 15 | 84 |
๊ธธ๋ | 87 | 84 | 76 |
๋ฏธ์ | 100 | 87 | 99 |
์์ด | 59 | 99 | 59 |
์ฒ ์ด | 46 | 77 | 56 |
numpy์์ ๋ฐฐ์ ๋ ์ ์นํ์ด ์ํค๋ transpose() ๋๋ .T๋ฅผ ์ฌ์ฉํ์ฌ ํ๊ณผ ์ด์ ๊ตํํ ์๋ ์๋ค.
๊ทธ๋ฌ๋ ๋ฐ์ดํฐํ๋ ์ ์๋ณธ์๋ ๋ฐ์๋์ง ์๋๋ค.
d4 = d3.transpose()
d4
์ฒ ์ | ์์ด | ๊ธธ๋ | ๋ฏธ์ | ์์ด | ์ฒ ์ด | |
---|---|---|---|---|---|---|
๊ตญ์ด | 84 | 21 | 87 | 100 | 59 | 46 |
์ํ | 87 | 15 | 84 | 87 | 99 | 77 |
์์ด | 78 | 84 | 76 | 99 | 59 | 56 |
d3
๊ตญ์ด | ์ํ | ์์ด | |
---|---|---|---|
์ฒ ์ | 84 | 87 | 78 |
์์ด | 21 | 15 | 84 |
๊ธธ๋ | 87 | 84 | 76 |
๋ฏธ์ | 100 | 87 | 99 |
์์ด | 59 | 99 | 59 |
์ฒ ์ด | 46 | 77 | 56 |
๋ฐ์ดํฐํ๋ ์ ํ์ธ#
๋ฐ์ดํฐํ๋ ์์ ์ ๋ณด๋ฅผ ํ์ธํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ ๋ณ์ ๋๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
.shape: ๋ชจ์์ ํ์ธํ๋ค.
.columns: ์ด์ด๋ฆ์ ํ์ธํ๋ค.
head(): ๋ฐ์ดํฐํ๋ ์์ ๋งจ ์ 5๊ฐ ํ์ ๋ณด์ฌ์ค๋ค.
tail(): ๋ฐ์ดํฐํ๋ ์์ ๋งจ ์๋ 5๊ฐ ํ์ ๋ณด์ฌ์ค๋ค.
info(): ์นผ๋ผ๋ช , Non-Null count, Dtype ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค๋ค.
isnull().sum(): ๊ฐ์ด ์๋(null) ๊ฐฏ์๋ฅผ ๋ฐํํ๋ค.
url='https://raw.githubusercontent.com/HaesunByun/Basic-Computing/main/titanic_test.csv'
titanic_test = pd.read_csv(url)
titanic_test
passenger ID
Pclass: ํฐ์ผ ํด๋์ค. (1 = 1st, 2 = 2nd, 3 = 3rd)
Name: ์ด๋ฆ
Sex: ์ฑ๋ณ
Age: ๋์ด(์ธ)
Sibsp (Siblings and spouse): ํจ๊ป ํ์นํ ํ์ ์๋งค, ๋ฐฐ์ฐ์ ์ ์ดํฉ
Parch (Parents and children): ํจ๊ป ํ์นํ ๋ถ๋ชจ, ์๋ ์ ์ดํฉ
Ticket: ํฐ์ผ ๋ฒํธ
Fare: ํ์น ์๊ธ
Cabin: ๊ฐ์ค ๋ฒํธ
Embarked: ํ์น ํญ๊ตฌ
# titanic_test.shape
# titanic_test.head(3)
# titanic_test.tail()
# titanic_test.info()
# titanic_test.isnull().sum()
๋ฐ์ดํฐํ๋ ์ ์ธ๋ฑ์ฑ๊ณผ ์ฌ๋ผ์ด์ฑ#
.iloc[]#
iloc๋ index location์ ์ฝ์๋ก ์์น ์ธ๋ฑ์ค๋ก๋ง ์ธ๋ฑ์ฑ๊ณผ ์ฌ๋ผ์ด์ฑ์ ํ ์ ์๋ค.
.iloc[]์ ์ฌ์ฉํ์ฌ ํ์ถ์ถ/์ด์ถ์ถ/ํ๋ ฌ์ถ์ถ์ ํด๋ณด์.
ํ์์ ๋ค์๊ณผ ๊ฐ๋ค.
๋ฐ์ดํฐํ๋ ์๋ช
.iloc[ํ ๋๋ ํ๋ฒ์, ์ด ๋๋ ์ด๋ฒ์]
์ด๋ ํ์ด๋ ์ด์ด๋ ๋ฒ์๊ฐ ์๋ ํ๋๋ง ๋ช ์ํ๋ค๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ ์๋ฆฌ์ฆ๋ก ๋์จ๋ค.
ํ๊ณผ ์ด ๋ชจ๋ ๋ฒ์๋ก ๋ช ์ํ๋ค๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ๋์จ๋ค.
ํ์ถ์ถ
d3.iloc[2]
d3.iloc[2:3]
๊ตญ์ด | ์ํ | ์์ด | |
---|---|---|---|
๊ธธ๋ | 87 | 84 | 76 |
์ด์ถ์ถ
d3.iloc[:, 1]
#d3.iloc[:, 0:2]
์ฒ ์ 87
์์ด 15
๊ธธ๋ 84
๋ฏธ์ 87
์์ด 99
์ฒ ์ด 77
Name: ์ํ, dtype: int64
ํ์ด ์ถ์ถ
d3.iloc[1:3, 0:2]
๊ตญ์ด | ์ํ | |
---|---|---|
์์ด | 21 | 15 |
๊ธธ๋ | 87 | 84 |
.loc[]#
์ค์ ์ธ๋ฑ์ค๋ก๋ง ์ธ๋ฑ์ฑ๊ณผ ์ฌ๋ผ์ด์ฑ์ ํ ์ ์๋ค.
์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
๋ฐ์ดํฐํ๋ ์๋ช
.loc[ํ ๋๋ ํ๋ฒ์, ์ด ๋๋ ์ด๋ฒ์]
์ด๋ ํ์ด๋ ์ด์ด๋ ๋ฒ์๊ฐ ์๋ ํ๋๋ง ๋ช ์ํ๋ค๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ ์๋ฆฌ์ฆ๋ก ๋์จ๋ค.
ํ๊ณผ ์ด ๋ชจ๋ ๋ฒ์๋ก ๋ช ์ํ๋ค๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ๋์จ๋ค.
ํ์ถ์ถ
d3.loc['๊ธธ๋']
d3.loc['๊ธธ๋':'์์ด']
๊ตญ์ด | ์ํ | ์์ด | |
---|---|---|---|
๊ธธ๋ | 87 | 84 | 76 |
๋ฏธ์ | 100 | 87 | 99 |
์์ด | 59 | 99 | 59 |
์ด์ถ์ถ
d3.loc[:, '๊ตญ์ด']
d3.loc[:, '๊ตญ์ด':'์ํ']
๊ตญ์ด | ์ํ | |
---|---|---|
์ฒ ์ | 84 | 87 |
์์ด | 21 | 15 |
๊ธธ๋ | 87 | 84 |
๋ฏธ์ | 100 | 87 |
์์ด | 59 | 99 |
์ฒ ์ด | 46 | 77 |
ํ๋ ฌ์ถ์ถ
d3.loc['์ฒ ์':'์์ด', '๊ตญ์ด':'์ํ']
๊ตญ์ด | ์ํ | |
---|---|---|
์ฒ ์ | 84 | 87 |
์์ด | 21 | 15 |
๋ฐ์ดํฐํ๋ ์๋ช #
๋ฐ์ดํฐํ๋ ์๋ช ๋ง ์ฌ์ฉํ์ฌ ์ถ์ถํ๋ ๋ฐฉ๋ฒ์ด ์กฐ๊ธ ๋ณต์กํ๋ค.
ํ์ถ์ถ์ ํ๋์ ํ์ ์ถ์ถํ๋๋ผ๋ ๋ฐ๋์ ์ฌ๋ผ์ด์ฑ์ผ๋ก ํด์ผ ํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์ด์ด๋ฆ์ผ๋ก ์ธ์ํด ์๋ฌ๊ฐ ๋๋ค.
๋ฐ์ดํฐํ๋ ์๋ช
[ํ๋ฒ์]
d3[2:3]
d3[2:5]
d3['๊ธธ๋':'๊ธธ๋']
d3['๊ธธ๋':'์ฒ ์ด']
๊ตญ์ด | ์ํ | ์์ด | |
---|---|---|---|
๊ธธ๋ | 87 | 84 | 76 |
๋ฏธ์ | 100 | 87 | 99 |
์์ด | 59 | 99 | 59 |
์ฒ ์ด | 46 | 77 | 56 |
์ด์ถ์ถ์ ๋ฐ๋์ ์นผ๋ผ๋ช ์ผ๋ก๋ง ์ถ์ถ๊ฐ๋ฅํ๋ค.
์ฌ๋ฌ ์นผ๋ผ์ ์ถ์ถํ ๋์๋ ๋ฆฌ์คํธ๋ก ๋ฃ์ด์ค๋ค.
๋ฐ์ดํฐํ๋ ์๋ช
[์ด์ด๋ฆ ๋๋ ์ด์ด๋ฆ ๋ฆฌ์คํธ]
d3['๊ตญ์ด']
d3[['๊ตญ์ด','์ํ']]
๊ตญ์ด | ์ํ | |
---|---|---|
์ฒ ์ | 84 | 87 |
์์ด | 21 | 15 |
๊ธธ๋ | 87 | 84 |
๋ฏธ์ | 100 | 87 |
์์ด | 59 | 99 |
์ฒ ์ด | 46 | 77 |
ํ๋ ฌ์ถ์ถ์ ํ๋ ฌ์ ๋๊ดํธ ์์๋ฅผ ๋ฐ๊ฟ ์ ์์ง๋ง ํ์ถ์ถ, ์ด์ถ์ถ ์ ์ฝ์ฌํญ์ด ๊ทธ๋๋ก ์ ์ฉ๋๋ค.
ํ์ถ์ถ์ ์ฌ๋ผ์ด์ฑ์ผ๋ก๋ง!
์ด์ถ์ถ์ ์นผ๋ผ๋ช ์ผ๋ก๋ง!
์ฌ๋ฌ ์ด์ ์ถ์ถํ ๋์๋ ๋ฆฌ์คํธ๋ก๋ง!
๋ฐ์ดํฐํ๋ ์๋ช
[ํ๋ฒ์][์ด์ด๋ฆ ๋๋ ์ด์ด๋ฆ ๋ฆฌ์คํธ]
๋ฐ์ดํฐํ๋ ์๋ช
[์ด์ด๋ฆ ๋๋ ์ด์ด๋ฆ ๋ฆฌ์คํธ][ํ๋ฒ์]
d3['๊ธธ๋':'๋ฏธ์'][['๊ตญ์ด','์ํ']]
d3[2:4][['๊ตญ์ด','์ํ']]
d3[['๊ตญ์ด','์ํ']]['๊ธธ๋':'๋ฏธ์']
d3[['๊ตญ์ด','์ํ']][2:4]
๊ตญ์ด | ์ํ | |
---|---|---|
๊ธธ๋ | 87 | 84 |
๋ฏธ์ | 100 | 87 |
๋ฐ์ดํฐํ๋ ์ ํ/์ด ์ถ๊ฐ, ์์ , ์ญ์ #
โ์ปด๊ธฐโ ์ด์ ์ถ๊ฐํด๋ณด์.
โ์ปด๊ธฐโ๋ผ๋ ์นผ๋ผ์ด ์์ผ๋ฉด ์ถ๊ฐ๋๊ณ , ์์ผ๋ฉด ์ ๋ฐ์ดํธ ๋๋ค.
comScore=[60, 70, 56, 74, 77, 66]
d3['์ปด๊ธฐ']= comScore
d3
๊ตญ์ด | ์ํ | ์์ด | ์ปด๊ธฐ | |
---|---|---|---|---|
์ฒ ์ | 84 | 87 | 78 | 60 |
์์ด | 21 | 15 | 84 | 70 |
๊ธธ๋ | 87 | 84 | 76 | 56 |
๋ฏธ์ | 100 | 87 | 99 | 74 |
์์ด | 59 | 99 | 59 | 77 |
์ฒ ์ด | 46 | 77 | 56 | 66 |
โํฉ๊ณโํ์ ์ถ๊ฐํด๋ณด์.
โํฉ๊ณโํ์ ๋ค์ด๊ฐ ๊ฐ์ sum()ํจ์๋ก ๊ตฌํ๋ค.
sum()ํจ์ ๊ดํธ ์์๋ axis(์ถ)์ ๋ช ์ํ ์ ์๋ค.
axis=0์ ํ, axis=1์ ์ด์ ์๋ฏธํ๋ค.
axis๋ฅผ ์๋ตํ๋ฉด ๊ธฐ๋ณธ๊ฐ์ 0์ด๋ค.
d3.loc['ํฉ๊ณ']=d3.sum()
d3
๊ตญ์ด | ์ํ | ์์ด | ์ปด๊ธฐ | |
---|---|---|---|---|
์ฒ ์ | 84 | 87 | 78 | 60 |
์์ด | 21 | 15 | 84 | 70 |
๊ธธ๋ | 87 | 84 | 76 | 56 |
๋ฏธ์ | 100 | 87 | 99 | 74 |
์์ด | 59 | 99 | 59 | 77 |
์ฒ ์ด | 46 | 77 | 56 | 66 |
ํฉ๊ณ | 397 | 449 | 452 | 403 |
๐ ๊ฐ ํ์ ๋ณ โ์ด์ โ ์ด์ ์ถ๊ฐํด ์ฃผ์ธ์.
๐ โ๊ธธ๋โ์ ์ปด๊ธฐ ์ ์๋ฅผ 86์ ์ผ๋ก ์์ ํด ์ฃผ์ธ์.
๐ ๊ธธ๋์ ์ด์ ๋ ๋ค์ ๊ณ์ฐํด ์ฃผ์ธ์.
๐ โ๊ฐ์ขโ์ด์ ์ถ๊ฐํ๊ณ โ001โ์ ๊ฐ์ผ๋ก ๋ฃ์ด์ฃผ์ธ์.
โํฉ๊ณโํ์ ์ญ์ ํด๋ณด์. .drop()์ผ๋ก๋ ์ญ์ ํ ๋ ์๋ณธ์ ๋ฐ์ํ๊ธฐ ์ํด์๋ inplace=True๋ฅผ ํด์ค์ผ ํ๋ค.
d3.drop('ํฉ๊ณ', axis=0, inplace=True)
d3
๊ตญ์ด | ์ํ | ์์ด | ์ปด๊ธฐ | |
---|---|---|---|---|
์ฒ ์ | 84 | 87 | 78 | 60 |
์์ด | 21 | 15 | 84 | 70 |
๊ธธ๋ | 87 | 84 | 76 | 56 |
๋ฏธ์ | 100 | 87 | 99 | 74 |
์์ด | 59 | 99 | 59 | 77 |
์ฒ ์ด | 46 | 77 | 56 | 66 |
๐ โ๊ฐ์ขโ์ด์ ์ญ์ ํด์ฃผ์ธ์.
๋ฐ์ดํฐํ๋ ์ ํ์ฉ์ฐ์ต#
์ต๋๊ฐ, ์ต์๊ฐ ๊ตฌํ๊ธฐ
print(d3['์ด์ '].max())
print(d3['์ด์ '].min())
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
D:\users\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2896 try:
-> 2897 return self._engine.get_loc(key)
2898 except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: '์ด์ '
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-45-9b8e3290d354> in <module>
----> 1 print(d3['์ด์ '].max())
2 print(d3['์ด์ '].min())
D:\users\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2978 if self.columns.nlevels > 1:
2979 return self._getitem_multilevel(key)
-> 2980 indexer = self.columns.get_loc(key)
2981 if is_integer(indexer):
2982 indexer = [indexer]
D:\users\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2897 return self._engine.get_loc(key)
2898 except KeyError:
-> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key))
2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2901 if indexer.ndim > 1 or indexer.size > 1:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: '์ด์ '
idxmax()๋ ์ต๋๊ฐ์ ์ธ๋ฑ์ค, idxmin()์ ์ต์๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ์ถ์ถํ๋ค.
print(d3['์ด์ '].idxmax())
print(d3['์ด์ '].idxmin())
โ์ด์ โ์ด 1๋ฑ์ธ ํ์์ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ค.
d3.loc[d3['์ด์ '].idxmax()]
์ด์ ์ด 200๋ณด๋ค ํฐ ๊ฒฐ๊ณผ๋ฅผ True, False๋ก ๋ง๋ ๋ค.
x=d3.loc[:, '์ด์ ']>200
x
d3['Pass/Fail']=x
d3
๋์ ๋๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ ๋งคํ์ ๋ณด๋ฅผ ์ฝ๊ฒ ๋ฃ์ ์ ์๋ค.
mapping = {True: 'Pass', False: 'Fail'}
d3['Pass/Fail']= d3['Pass/Fail'].map(mapping)
d3
d3['Pass/Fail'] = ['Pass' if total>200 else 'Fail' for total in d3['์ด์ ']]
d3
groupby()๋ฅผ ํตํด์ ๊ทธ๋ฃน์ผ๋ก ๋ฌถ์ด์ค ์ ์๋ค.
total_groupby = d3.groupby('Pass/Fail')
total_groupby.size()
์๋๋ ๊ฒฐ์ธก๊ฐ์ ์ฑ์ฐ๋ ๋ฐฉ๋ฒ์ ์ตํ๊ธฐ ์ํด ์ผ๋ถ๋ฌ ๊ฒฐ์ธก๊ฐ์ ๋ง๋ ๊ฒฝ์ฐ์ด๋ค.
mapping = {'Pass': 300000}
d3['์ฅํ๊ธ']=d3['Pass/Fail'].map(mapping)
d3
๊ฒฐ์ธก๊ฐ์ ์ฑ์ฐ๋ ํจ์๋ .fillna()์ด๋ค.
d3['์ฅํ๊ธ'].fillna(0, inplace=True)
d3
sort_index()๋ฅผ ํตํด ์ธ๋ฑ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค.
d3.sort_index(ascending=True)
ํน์ ์นผ๋ผ๋ช ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค.
d3.sort_values(by = '๊ตญ์ด', ascending=True)
๋ง๋ฌด๋ฆฌ#
pandas๋ ํ์ด์ฌ์์ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
Pandas๋ Series์ DataFrame์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ ๊ณตํ๋ค.
์๋ฆฌ์ฆ๋ 1์ฐจ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ตฌ์กฐ์ด๋ค.
DataFrame์ ํ๊ณผ ์ด๋ก ์ด๋ฃจ์ด์ง 2์ฐจ์ ํํ์ ๋ฐฐ์ด๋ก, ์๋ฆฌ์ฆ๋ค์ ๋ฌถ์์ด๋ค.
loc๋ฅผ ์ด์ฉํ ํ์ด์ ํ : ๋ฐ์ดํฐํ๋ ์๋ช .loc[index๋ช , column๋ช ]
iloc๋ฅผ ์ด์ฉํ ํ์ด์ ํ : ๋ฐ์ดํฐํ๋ ์๋ช .iloc[index๋ฒํธ, column๋ฒํธ]