Pandas ์ฌ์ฉ๋ฒ - Series์ DataFrame [๊ธฐ๋ณธ]
๋ฐ์ดํฐ ๋ถ์์ ํ๋ค ๋ณด๋ฉด Pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์์ ์ผ๋ก ์ฌ์ฉํ๋ค. Pandas๋ ๋ฐ์ดํฐ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ๋ฐ ์ํค๋ฐฑ๊ณผ์์๋ ๋ค์๊ณผ ๊ฐ์ด ๋ํ๋ด๊ณ ์๋ค.
Pandas๋ ๋ฐ์ดํฐ ์กฐ์ ๋ฐ ๋ถ์์ ์ํ Python ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ์ฉ์ผ๋ก ์์ฑ๋ ์ํํธ์จ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ๋๋ค. ํนํ ์ซ์ ํ ์ด๋ธ๊ณผ ์๊ณ์ด ์ ์กฐ์ํ๊ธฐ ์ํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ ์ฐ์ฐ์ ์ ๊ณตํฉ๋๋ค.
pandas - Python Data Analysis Library
pandas pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language. Install pandas now!
pandas.pydata.org
ํ์๋ Pandas๋ฅผ ๋ฐ๋ก ๊ณต๋ถํ๊ธฐ ๋ณด๋จ ๊ทธ๋ฅ ๋ฐ์ดํฐ ๋ถ์์ ํ๋ฉด์ ์ด๊ฒ์ ๊ฒ ์ฐ๊ฒ ๋์๋๋ฐ, ์ด๋ฒ ๊ธฐํ์ ์ฃผ์ ๊ธฐ๋ฅ๋ค์ ๋ํ ๊ฒ๋ค์ ์ ๋ฆฌํ๋ ค ํ๋ค.
1. Series - 1์ฐจ์ ๋ฐ์ดํฐ (์ ์, ์ค์, ๋ฌธ์์ด ๋ฑ)
Series๋ 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๋ฏธํ๋ค.
>>> import pandas as pd
>>> series1 = pd.Series([-20,-10,10,20])
>>> print(series1)
0 -20
1 -10
2 10
3 20
dtype: int64
๊ฐ๋จํ Series๋ ์์๊ฐ์ด ์์ฑํ ์ ์๋ค. ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ 1์๋ถํฐ 4์๊น์ง ํ๊ท ์จ๋์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ Series๋ก ํํํ ์ ์๋ค. Index๋ ์ค์ ๊ฐ๋ฅํ๋ค.
>>> series1 = pd.Series([-20,-10,10,20], index = ['Jan', 'Feb', 'Mar', 'Apr'])
>>> print(Series1)
Jan -20
Feb -10
Mar 10
Apr 20
dtype: int64
Pandas Series๊ฐ ์ ๊ณตํ๋ ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
Pandas์ Series๋ฅผ ์์ฑํ ๋ dtype์ ์ค์ ํ ์ ์๋๋ฐ, ์๊น์ ๊ฐ์ ํ๊ท ์จ๋ ๊ฐ์ ์ ์๊ฐ ์๋ string ํํ๋ก ์ค์ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๊ฒฐ๊ณผ๊ฐ ๋ํ๋๋ค.
>>> import pandas as pd
>>> temp = pd.Series([-20,-10,10,20],dtype = 'str')
>>> print(temp)
0 -20
1 -10
2 10
3 20
dtype: object
Pandas ์๋ฃํ์์ Object๋ ๋ฌธ์์ด์ ์๋ฏธํ๋ค. ํท๊ฐ๋ฆฌ์ง ๋ง์!
๊ฐ parameter์ ๋ํ ์ค๋ช ์ ๊ณต์ Document๋ฅผ ์ฐธ๊ณ ํ์!
https://pandas.pydata.org/docs/reference/api/pandas.Series.html
pandas.Series — pandas 1.3.5 documentation
Values must be hashable and have the same length as data. Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, …, n) if not provided. If data is dict-like and index is None, then the keys in the data are used as the index. If the ind
pandas.pydata.org
2. DataFrame - 2์ฐจ์ ๋ฐ์ดํฐ (Series ๋ค์ ๋ชจ์)
DataFrame์ 2์ฐจ์ ๋ฐ์ดํฐ๋ก Series๋ค์ด ๋๊ฐ ์ด์ ๋ชจ์ธ ๊ฒ์ด๋ค.
์๊น์ ๊ฐ์ด 1~4์์ ์จ๋ ๋ฟ๋ง ์๋๋ผ ์ต๋, ๋ ์จ, ์ผ์ถ ์๊ฐ ๋ฑ๋ฑ...์ ๋ฐ์ดํฐ๊ฐ ์ถ๊ฐ๋ก ๋ ์๋ค๋ฉด DataFrame์ ๋ง๋ค ์ ์๋ ๊ฒ์ด๋ค.
>>> temp = pd.DataFrame([[-20,-10,10,20],[64,32,53,12],['๋ง์','ํ๋ฆผ','๋น','๋ง์']], columns = ['Jan','Feb','Mar','Apr'],index = ['์จ๋','์ต๋','๋ ์จ'])
>>> temp
Jan Feb Mar Apr
์จ๋ -20 -10 10 20
์ต๋ 64 32 53 12
๋ ์จ ๋ง์ ํ๋ฆผ ๋น ๋ง์
๊ฐ์ฒด๋ฅผ ์ค์ ํ๊ณ , ์ํ๋ ๊ฐ๋ค์ 2์ฐจ์ ๋ฐฐ์ด ํํ๋ก ์ ๋ ฅํ์ฌ DataFrame์ ๋ง๋ค ์ ์๋ค. ํน์ ์ฌ์ (dictionary) ์๋ฃ๊ตฌ์กฐ๋ฅผ ํตํด ์์ฑํ ์๋ ์๋ค.
>>> data = {
'์ด๋ฆ' : ['์ฑ์น์', '์ ๋๋ง', '์กํ์ญ', '์ํ์
', '๊ฐ๋ฐฑํธ', '๋ณ๋๊ท', 'ํฉํ์ฐ', '์ค๋ํ'],
'ํ๊ต' : ['๋ถ์ฐ๊ณ ', '๋ถ์ฐ๊ณ ', '๋ถ์ฐ๊ณ ', '๋ถ์ฐ๊ณ ', '๋ถ์ฐ๊ณ ', '๋ฅ๋จ๊ณ ', '๋ฅ๋จ๊ณ ', '๋ฅ๋จ๊ณ '],
'ํค' : [197, 184, 168, 187, 188, 202, 188, 190],
'๊ตญ์ด' : [90, 40, 80, 40, 15, 80, 55, 100],
'์์ด' : [85, 35, 75, 60, 20, 100, 65, 85],
'์ํ' : [100, 50, 70, 70, 10, 95, 45, 90],
'๊ณผํ' : [95, 55, 80, 75, 35, 85, 40, 95],
'์ฌํ' : [85, 25, 75, 80, 10, 80, 35, 95],
'SWํน๊ธฐ' : ['Python', 'Java', 'Javascript', '', '', 'C', 'PYTHON', 'C#']
}
>>> df = pd.DataFrame(data)
>>> print(df)
๋ค์๊ณผ ๊ฐ์ด DataFrame์ด ์์ฑ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ถ๊ฐ๋ก ์ด๋ ๊ฒ ์์ฑ๋ DataFrame ๋ด์ Data์ ์ ๊ทผํ ์ ์๋ค.
df['์ด๋ฆ']
df[['์ด๋ฆ','ํค']] #ํ๋์ ์ด๋ง ๊ฐ์ง๊ณ ์ค๋ ค๋ฉด ํ๋๋ง ์ ์ผ๋ฉด ๋์ง๋ง ๋๊ฐ ์ด์ ๊ฐ์ง๊ณ ์ค๋ ค๋ฉด ๋ฆฌ์คํธ๋ก ๋ฌถ์ด์ค์ผํจ
ํ๋์ ์ด๋ง ๊ฐ์ ธ์ค๋ ค๋ฉด df๋ค์ []๋ฅผ ์ฌ์ฉํ์ฌ ์ด์ ์ด๋ฆ์ ์ ์ผ๋ฉด ๋์ง๋ง, ์ฌ๋ฌ ๊ฐ์ ์ด์ ๊ฐ์ ธ์ค๋ ค๋ฉด ๋ฐฐ์ด๋ก ๋ฌถ์ ํ ์ ๋ ฅํด์ผ ํ๋ค. ์ผ์ชฝ์ 0~7์ ์ซ์๊ฐ ๋ง์์ ์๋ ๋ค๋ฉด index๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋๋ค.
#DataFrame ๊ฐ์ฒด ์์ฑ (Index ์ง์ )
df = pd.DataFrame(data,index = ['1๋ฒ','2๋ฒ','3๋ฒ','4๋ฒ','5๋ฒ','6๋ฒ','7๋ฒ','8๋ฒ'])
df
index์ ์ด๋ฆ์ ์ค์ ํ๊ณ ์ถ๋ค๋ฉด ?
##index ์ด๋ฆ ์ค์
df.index.name = '์ง์๋ฒํธ'
df
์ธ๋ฑ์ค์ ์ด๋ฆ์ด ์ง์๋ฒํธ๋ก ์ค์ ๋์๋ค. ๊ทผ๋ฐ ์ด์ ์ฒ๋ผ 0~7์ ์ซ์๋ฅผ ๋ค์ ๋ถ์ด๊ณ ์ถ๋ค๋ฉด?
#index ์ด๊ธฐํ
df.reset_index()
๋ค์๊ณผ ๊ฐ์ด ์ธ๋ฑ์ค๊ฐ ์ด๊ธฐํ ๋์๋ค. reset_index์ ๋ณ์ ์ค์๋ drop์ด ์๋๋ฐ, drop์ True๋ก ์ค์ ํ๋ฉด '์ง์๋ฒํธ'์ด์ด ์ญ์ ๋๋ค.
df.reset_index(drop=True, inplace=False) #์๋ ์ฐ๋ '์ง์๋ฒํธ' ์ธ๋ฑ์ค ์ญ์ -> ์ค์ ๋ฐ์ดํฐ์๋ ๋ฐ์์ด ์๋จ. ๋ฐ๋ผ์ ์ง๊ธ df๋ฅผ ์ถ๋ ฅํ๋ฉด ์์ ๊ฒฐ๊ณผ๋ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ
ํ์ฌ๋ '์ง์๋ฒํธ'๊ฐ ์ญ์ ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด์ง๋ง, df๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด ๊ทธ๋๋ก ์๋ค. ์ญ์ ๋ ์ํ๋ฅผ df์ ์ ์ฅํ๊ณ ์ถ๋ค๋ฉด inplace๋ฅผ True๋ก ํด์ฃผ๋ฉด ๋๋ค.
ํน์ ์๊ธฐ๊ฐ ์ํ๋ ์ด์ ์ธ๋ฑ์ค๋ก ์ค์ ํ ์ ์๋ค.
df.set_index('์ด๋ฆ',inplace=True)
์ด์ ๋ ์ฌ๋ ๋ณ๋ก ์ ๋ณด๋ฅผ ์ ์ ์๊ฒ ๋์๋ค. ํ์ง๋ง ์ ๋ ฌ์ด ์๋์ด ์์ผ๋ฏ๋ก.. ์ด๋ฅผ ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด sort_index๋ฅผ ์ฌ์ฉํ๋ค.
df.sort_index(ascending=True) #์ค๋ฆ์ฐจ์ ์ ๋ ฌ