1. Series

1. Series


๐ŸŒต ์‹œ๋ฆฌ์ฆˆ๋Š” ํ–‰ / ์—ด์˜ ๊ตฌ๋ถ„์ด ์—†์Šต๋‹ˆ๋‹ค.

๐ŸŒต index ์™€ values ๋งŒ์„ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐํ˜•์‹์ž…๋‹ˆ๋‹ค.

2. Series ์ƒ์„ฑ

2.1. index๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•ด์„œ ๋งŒ๋“ค๊ธฐ


๐ŸŒต pandas ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import

import pandas as pd

๐ŸŒต pd.Series(๋ฆฌ์ŠคํŠธ)

a = pd.Series(["a","b","c","d"])
a
>> 
0    a
1    b
2    c
3    d
dtype: object

์ธ๋ฑ์Šค๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ default ๊ฐ’์€ 0,1,2,3โ€ฆ ์œผ๋กœ ์ž๋™์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

๐ŸŒต .index( )

a.index = [4,5,6,7]
a
>> 
4    a
5    b
6    c
7    d
dtype: object

.index( ) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ๋‹ค์Šต๋‹ˆ๋‹ค.

2.2. index ๋™์‹œ ์„ ์–ธํ•˜๊ธฐ


๐ŸŒต pd.Series(๋ฆฌ์ŠคํŠธ, ์ธ๋ฑ์Šค๋ฆฌ์ŠคํŠธ)

b = pd.Series([1,2,3,4], index = ["A","B","C","D"])
b
>> 
A    1
B    2
C    3
D    4
dtype: int64

index์— ๋ฌธ์ž์—ด๋„ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. Series์— ์ ‘๊ทผํ•˜๊ธฐ

3.1. value ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ


๐ŸŒต ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ

์ƒˆ๋กœ์šด series ๋ฐ์ดํ„ฐ c๋ฅผ ์„ ์–ธํ•ด ๋ด…์‹œ๋‹ค.

c = pd.Series([1,2,3,4])
c
>> 
0    1
1    2
2    3
3    4
dtype: int64

[ ] ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” index 1์— ์ ‘๊ทผํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค!!

c[1] = "A"
c
>> 
0    1
1    A
2    3
3    4
dtype: object

๊ทธ ๊ฒฐ๊ณผ index 1์˜ value๊ฐ€ A๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ int64์˜€๋˜ ์ž๋ฃŒํ˜•์ด ์ˆซ์ž๋“ค ์‚ฌ์ด์— ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ€์„œ object๋กœ ๋ณ€ํ•œ ๊ฒƒ์„ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.

3.2. ๊ฐ’ ์‚ญ์ œํ•˜๊ธฐ


๐ŸŒต del

del c[1]
c
>>
0    1
2    3
3    4
dtype: object

index 1์˜ value๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3.3. ์ž๋ฃŒํ˜• ๋ณ€๊ฒฝํ•˜๊ธฐ


๐ŸŒต .astype(dtype)

c = c.astype("int64")
c
>> 
0    1
2    3
3    4
dtype: int64

object์˜€๋˜ ์ž๋ฃŒํ˜•์ด int64๊ฐ€ ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!

Leave a comment