[Pandas] Series (CRUD)
Contents
- Create
- 1-1) List to Series
- 1-2) Dict to Series
- Read & Update
- 2-1) Index์ Values
- 2-2) ์์ ์ ํ ๋ฐ ์์
- Delete
๋ณธ ํฌ์คํ ์ Pandas์ Series์ ๊ดํ์ฌ ์ ๋ฆฌํ์์ต๋๋ค.
pandas์ Series๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ ํน์ง์ ๊ฐ์ง๋ค.
- ๋ฐ์ดํฐ๊ฐ ์์ฐจ์ ์ผ๋ก ๋์ด๋ 1์ฐจ์ ๋ฐฐ์ด์ ํํ์ด๋ค.
index์ ๋ฐ์ดํฐvalue๊ฐ ์ผ๋์ผ๋ก ๋์๋๋ค.index๋ ๋ฐ์ดํฐvalue์ ์์น๋ฅผ ๋ํ๋ด๋ ์ฃผ์์ญํ ์ ํ๋ค.
Series ๊ฐ์ฒด๋ฅผ CRUD(Create, Read, Update, Delete)์์๋ก ์์๋ณด์.
Create
Series๊ฐ์ฒด๋ ๋์
๋๋ฆฌํน์ ๋ฆฌ์คํธ ๊ฐ์ฒด๋ฅผ ๋ค์์ ํจ์ ์ธ์์ ์ ๋ฌํ์ฌ ์์ฑํ ์ ์๋ค.
# Series์ ๋์๋ฌธ์์ ์ ์ํ์!
pandas.Series( [๋์
๋๋ฆฌ or ๋ฆฌ์คํธ], index = [์ธ๋ฑ์ค ๋ผ๋ฒจ ๋ฆฌ์คํธ])
1-1) Create : List to Series
๋ค์๊ณผ ๊ฐ์ด ๋ฆฌ์คํธ ๊ฐ์ฒด๋ฅผ Series์ ํจ์ ์ธ์์ ๊ธฐ์
ํ์ฌ ์์ฑํ ์ ์๋ค. ์ถ๋ ฅ๋ ๊ฒฐ๊ณผ์์ ์ข์ธก์ ๊ฒฝ์ฐ ํ์ฌ ๋ณ๋์ index๊ฐ์ ์ ๋ฌํ์ง ์์๊ธฐ ๋๋ฌธ์ ์ธ๋ฑ์ค์ ์ ์ํ ์์น ์ธ๋ฑ์ค(integer position)๊ฐ ์๋์ผ๋ก ์ง์ ๋ ๊ฒ์ ํ์ธํ ์ ์์ผ๋ฉฐ ์ฐ์ธก์ ๊ฒฝ์ฐ ๊ฐ ์ธ๋ฑ์ค์ ๋์๋๋ ๊ฐ์ด๋ค. ์ ์ํ ์์น ์ธ๋ฑ์ค์ ๊ฒฝ์ฐ 0๋ถํฐ ์๋ ์์ฑ๋๋ค.
์ด๋, ๋ฐ์ดํฐ ๊ฐ์ ์๋ฃํ(dtype)์ ์ ์ํ(int64)์ธ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๋๋ถ์ด, type()ํจ์๋ก list_to_sr์ ์ ์ฅ๋ ๊ฐ์ฒด์ ์๋ฃํ์ ํ์ธํด ๋ณด๋ฉด Series ํด๋์ค์ธ ๊ฒ์ ํ์ธํ ์ ์๋ค.
list_to_sr = pd.Series([1, 2, 3])
print(list_to_sr)
print(type(list_to_sr))
0 1
1 2
2 3
dtype: int64
<class 'pandas.core.series.Series'>
์์ ๋ฌ๋ฆฌ ๋ณ๋์ index๋ฆฌ์คํธ๋ฅผ ํจ์ ๋ด๋ถ์์ ์ง์ ํ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ ์์น ์ธ๋ฑ์ค๊ฐ ์๋ ์ธ๋ฑ์ค ๋ผ๋ฒจ(index label)์ด ์ง์ ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
list_to_sr = pd.Series([1, 2, 3], index = ['A', 'B', 'C'])
print(list_to_sr)
print(type(list_to_sr))
A 1
B 2
C 3
dtype: int64
<class 'pandas.core.series.Series'>
1-2) Create : Dict to Series
pandas์ series ํจ์์๋ ์๋ฆฌ์ฆ ๊ฐ์ฒด์ ๋น์ทํ๊ฒ key์ value๋ฅผ ์ฌ์ฉํ๋ dict๊ฐ์ฒด๋ ๊ธฐ์
ํ์ฌ ์์ฑํ ์ ์๋ค.
dict_to_sr = pd.Series({'A':1, 'B':2, 'C':3})
print(dict_to_sr)
print(type(dict_to_sr))
A 1
B 2
C 3
dtype: int64
<class 'pandas.core.series.Series'>
Read & Update
2-1) Index์ Values
Series๊ฐ์ฒด๋ ๋ค์์ ์์ฑ๋ค์ ํตํด์ ์ธ๋ฑ์ค๋ฐฐ์ด๊ณผ ๋ฐ์ดํฐ ๊ฐ ๋ฐฐ์ด๋ง์ ๋ฐ๋ก ์ ํํ ์ ์๋ค.
index: ์ธ๋ฑ์ค ๋ฐฐ์ด ์ ํ- Series๊ฐ์ฒด์ ์ธ๋ฑ์ค๊ฐ ์ ์ํ ์์น ์ธ๋ฑ์ค์ ๊ฒฝ์ฐ,
์ธ๋ฑ์ค ๋ฒ์์ธRangeIndex๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. - ์ธ๋ฑ์ค ๋ผ๋ฒจ์ด ์ง์ ๋์ด ์์ ๊ฒฝ์ฐ, ์๋ฆฌ์ฆ์ ์ธ๋ฑ์ค ๋ฐฐ์ด์ ๋ฐํํ๋ค.
- Series๊ฐ์ฒด์ ์ธ๋ฑ์ค๊ฐ ์ ์ํ ์์น ์ธ๋ฑ์ค์ ๊ฒฝ์ฐ,
values: ๋ฐ์ดํฐ๊ฐ ๋ฐฐ์ด ์ ํ- *
name: ์๋ฆฌ์ฆ์ ์ด๋ฆ์ ์ง์ ํ๋ค.
์ด๋ฅผ ์ด์ฉํ์ฌ, ์์์ ์์ฑ๋ dict_to_sr๊ณผ list_to_sr์ ์ธ๋ฑ์ค์ ๊ฐ์ ์ ๊ทผํ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ๋ค.
# ๋ฆฌ์คํธ
list_sr_index = list_to_sr.index
list_sr_value = list_to_sr.values
print('< ๋ฆฌ์คํธ to ์๋ฆฌ์ฆ >')
print(f'index : {list_sr_index}', '\n',
f'type(index) : {type(list_sr_index)}', '\n',
f'value : {list_sr_value }', '\n',
f'type(value) : {type(list_sr_value)}')
print() # ๊ฐํ
# ๋์
๋๋ฆฌ
dict_sr_index = dict_to_sr.index
dict_sr_value = dict_to_sr.values
print('< ๋์
๋๋ฆฌ to ์๋ฆฌ์ฆ >')
print(f'index : {dict_sr_index }', '\n',
f'type(index) : {type(dict_sr_index)}', '\n',
f'value : {dict_sr_value }', '\n',
f'type(value) : {type(dict_sr_value)}')
< ๋ฆฌ์คํธ to ์๋ฆฌ์ฆ >
index : RangeIndex(start=0, stop=3, step=1)
type(index) : <class 'pandas.core.indexes.range.RangeIndex'>
value : [1 2 3]
type(value) : <class 'numpy.ndarray'>
< ๋์
๋๋ฆฌ to ์๋ฆฌ์ฆ >
index : Index(['A', 'B', 'C'], dtype='object')
type(index) : <class 'pandas.core.indexes.base.Index'>
value : [1 2 3]
type(value) : <class 'numpy.ndarray'>
๋๋ถ์ด, index์์ฑ์ ์ด์ฉํด์ ์ธ๋ฑ์ค ๋ผ๋ฒจ์ ๋ณ๊ฒฝํ๊ฑฐ๋ ์ถ๊ฐํ ์ ์๋ค. ํ์ง๋ง, ์๋ฆฌ์ฆ value์ ๊ฒฝ์ฐ values์์ฑ์ผ๋ก ์์ ์ด ๋ถ๊ฐ๋ฅํ๋ฉฐ, ๋ง์ฝ ์๋ํ ๊ฒฝ์ฐ AttributeError๋ฅผ ๋ฐํํ๋ค.
exam_sr = pd.Series({'A':1, 'B':2, 'C':3})
print(f'๊ธฐ๋ณธ : {exam_sr.index}')
exam_sr.index = ['๊ฐ', '๋', '๋ค']
print(f'\n์ธ๋ฑ์ค ๋ณ๊ฒฝ : {exam_sr.index}')
# AttributeError๋ฐ์
try:
exam_sr.values = [100, 200, 300]
print(f'\n๋ฐ์ดํฐ ๊ฐ ๋ณ๊ฒฝ : {exam_sr.values}')
except AttributeError as e:
print('\n','AttributeError :', e)
๊ธฐ๋ณธ : Index(['A', 'B', 'C'], dtype='object')
์ธ๋ฑ์ค ๋ณ๊ฒฝ : Index(['๊ฐ', '๋', '๋ค'], dtype='object')
AttributeError : can't set attribute
2-2) ์์ ์ ํ ๋ฐ ์์
์์์ ์ฃผ์๋ฅผ ์๋ฏธํ๋ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ์ฌ ์๋ฆฌ์ฆ์ ์์๋ฅผ ํ๋๋ง ์ ํํ๊ฑฐ๋ ์ฌ๋ฌ ์์๋ฅผ ํ๊บผ๋ฒ์ ์ ํํ ์ ์๋ค.
- ์ ์ํ ์์น ์ธ๋ฑ์ค๋ก ์ง์ ๋ ์๋ฆฌ์ฆ์ ๊ฒฝ์ฐ ๋๊ดํธ[]์์ ์์น๋ฅผ ๋ํ๋ด๋ ์ซ์๋ฅผ ์ ๋ ฅํ๋ค.
- ์ธ๋ฑ์ค ๋ผ๋ฒจ์ด ์ง์ ๋ ์๋ฆฌ์ฆ์ ๊ฒฝ์ฐ ๋๊ดํธ[]์์ ์ซ์๋ฅผ ์ ๋ ฅํ๊ฑฐ๋ ์ด๋ฆ๊ณผ ํจ๊ป ๋ฐ์ดํ๋ฅผ ์ ๋ ฅํ๋ค.
๊ทธ๋ฆฌ๊ณ , ์ฌ๋ฌ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๋ฆฌ์คํธ ํํ๋ก ๋๊ดํธ[]์์ ์ ๋ ฅํ๋ฉด ๊ฐ ์ธ๋ฑ์ค์ ๋์๋๋ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ๋ฐํํ๋ค. ๊ทธ๋ฆฌ๊ณ , ์ธ๋ฑ์ค ๋ฒ์๋ฅผ ์ฌ๋ผ์ด์ฑํ์ฌ ์ ํํ ์๋ ์๋ค.
์ ์ํ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ ๊ฒฝ์ฐ์๋ ํ์ด์ฌ์ ๋ฆฌ์คํธ ์ฌ๋ผ์ด์ฑ๊ณผ ๋์ผํ๊ฒ [0: n]์ผ๋ก ์ฌ๋ผ์ด์ฑํ ๊ฒฝ์ฐ n-1๊น์ง๋ง ๋ฐํํ๋ค. ํ์ง๋ง, ์ธ๋ฑ์ค ๋ผ๋ฒจ์ ์ด์ฉํ์ฌ [โAโ:โDโ]์ผ๋ก ์ฌ๋ผ์ด์ฑํ ๊ฒฝ์ฐ โDโ๊น์ง ๋ฐํ๋๋ค.
exam_sr = pd.Series({'A':1, 'B':2, 'C':3})
# ์ ์ํ ์์น ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ์ฌ ์์ธ
a0 = exam_sr[0] # ํ๊ฐ์ ์์
a1 = exam_sr[[0, 2]] # n๊ฐ์ ์์
a2 = exam_sr[0:2] # ์ฌ๋ผ์ด์ฑ
print(f'ํน์ ์์ 1๊ฐ ์ ํ : \n{a0}\n')
print(f'ํน์ ์์ n๊ฐ ์ ํ : \n{a1}\n')
print(f'์ฌ๋ผ์ด์ฑ์ผ๋ก ์์ ์ ํ : \n{a2}\n')
ํน์ ์์ 1๊ฐ ์ ํ :
1
ํน์ ์์ n๊ฐ ์ ํ :
A 1
B 2
dtype: int64
์ฌ๋ผ์ด์ฑ์ผ๋ก ์์ ์ ํ :
A 1
B 2
dtype: int64
exam_sr = pd.Series({'A':1, 'B':2, 'C':3})
# ์ธ๋ฑ์ค ๋ผ๋ฒจ์ ์ด์ฉํ์ฌ ์์ธ
a0 = exam_sr['A'] # ํ๊ฐ์ ์์
a1 = exam_sr[['A', 'B']] # n๊ฐ์ ์์
a2 = exam_sr['A':'B'] # ์ฌ๋ผ์ด์ฑ
print(f'ํน์ ์์ 1๊ฐ ์ ํ : \n{a0}\n')
print(f'ํน์ ์์ n๊ฐ ์ ํ : \n{a1}\n')
print(f'์ฌ๋ผ์ด์ฑ์ผ๋ก ์์ ์ ํ : \n{a2}\n')
ํน์ ์์ 1๊ฐ ์ ํ :
1
ํน์ ์์ n๊ฐ ์ ํ :
A 1
B 2
dtype: int64
์ฌ๋ผ์ด์ฑ์ผ๋ก ์์ ์ ํ :
A 1
B 2
dtype: int64
๋๋ถ์ด, ์ด๋ฅผ ์ด์ฉํ์ฌ values ์์ฑ์ด ํ ์ ์์๋ ์๋ฆฌ์ฆ์ ๋ฐ์ดํฐ ๊ฐ์ ์์ ํ ์ ์๋ค.
exam_sr = pd.Series({'A':1, 'B':2, 'C':3})
print(f'๊ธฐ๋ณธ : {exam_sr.values}')
exam_sr[0] = 100
print(f'ํ ๊ฐ์ ๊ฐ๋ง ์์ : {exam_sr.values}')
exam_sr[[0, 1]] = [10, 20]
print(f'n๊ฐ์ ๊ฐ๋ง ์์ : {exam_sr.values}')
exam_sr[0:3] = [10, 20, 30]
print(f'n๊ฐ์ ๊ฐ๋ง ์์ : {exam_sr.values}')
๊ธฐ๋ณธ : [10 20 3]
ํ ๊ฐ์ ๊ฐ๋ง ์์ : [100 2 3]
n๊ฐ์ ๊ฐ๋ง ์์ : [10 20 3]
์ฌ๋ผ์ด์ฑ์ ์ด์ฉํ ์์ : [10 20 30]
Delete
์๋ฆฌ์ฆ์ ๋ฐ์ดํฐ๋ ๋ค์์ ์ฌ์ฉํ์ฌ ์ ๊ฑฐํ ์ ์๋ค.
del๋ช ๋ น์ด- ์ธ๋ฑ์ค ๋ผ๋ฒจ์ด ์ง์ ๋ ์๋ฆฌ์ฆ์ผ ๊ฒฝ์ฐ์๋
series๊ฐ์ฒด[index ๋ฒํธ]์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค
- ์ธ๋ฑ์ค ๋ผ๋ฒจ์ด ์ง์ ๋ ์๋ฆฌ์ฆ์ผ ๊ฒฝ์ฐ์๋
drop๋ฉ์๋inplace = True:๊ธฐ์กด ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.inplace = False:์๋ก์ด ๊ฐ์ ๋ฐํํ๋ค. (Default)- ์ธ๋ฑ์ค ๋ผ๋ฒจ์ด ์ง์ ๋ ์๋ฆฌ์ฆ์ผ ๊ฒฝ์ฐ์๋
series๊ฐ์ฒด[index ๋ฒํธ]์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค
exam_sr = pd.Series({'A':1, 'B':2, 'C':3})
print(f'< ๊ธฐ๋ณธ >\n {exam_sr}\n')
sr1 = exam_sr.copy()
del sr1['A']
print(f'< del ๋ช
๋ น์ด๋ฅผ ์ด์ฉํ ๋ฐ์ดํฐ ์ ๊ฑฐ >\n {sr1}\n')
sr2 = exam_sr.copy()
sr2.drop('B', inplace = True)
print(f'< drop : ๋จ์ผ ๋ฐ์ดํฐ ์ ๊ฑฐ >\n {sr2}\n')
sr3 = exam_sr.copy()
sr3.drop(['A', 'C'], inplace = True)
print(f'< drop : ๋ค์ค ๋ฐ์ดํฐ ์ ๊ฑฐ >\n {sr3}\n')
< ๊ธฐ๋ณธ >
A 1
B 2
C 3
dtype: int64
< del ๋ช
๋ น์ด๋ฅผ ์ด์ฉํ ๋ฐ์ดํฐ ์ ๊ฑฐ >
B 2
C 3
dtype: int64
< drop : ๋จ์ผ ๋ฐ์ดํฐ ์ ๊ฑฐ >
A 1
C 3
dtype: int64
< drop : ๋ค์ค ๋ฐ์ดํฐ ์ ๊ฑฐ >
B 2
dtype: int64
