import pandas as pd
선언
s1 = pd.Series([1, 3, np.nan, 2, 4])
s2 = pd.Series(np.arange(10))
dic3 = {"D":500, "E":400, "F":300}
s3 = pd.Series(dic3)
index 설정
s = pd.Series(np.random.randint(10, size=5), index=list("ABCDE"))
A 6
B 3
c 0
D 8
E 4
데이터 확인
list(s.index), list(s.values)
(['A', 'B', 'C', 'D', 'E'], [6, 3, 0, 8, 4])
string = "B"
s.B, s["B"], s[string]
(3, 3, 3)
이름 설정
s.name = "random_number_series"
s.index.name = "id"
s
id
A 6
B 3
C 0
D 8
E 4
Name: random_number_series, dtype: int64
연산(브로드캐스팅)
s * 5
A 6
B 3
C 0
D 8
E 4
데이터 선택
s[list("BCD")]
s[s>5]
B 3
C 0
D 8
A 6
D 8
offset
s[::-1]
E 4
D 8
C 0
B 3
A 6
NaN data
sum_s_s3 = s + s3
A NaN
B NaN
C NaN
D 508.0
E 404.0
F NaN
sum_s_s3.isnull()
A True
B True
C True
D False
E False
F True
dtype: bool
sum_s_s3[sum_s_s3.isnull()] = s
A 6.0
B 3.0
C 0.0
D 508.0
E 404.0
F NaN
sum_s_s3[sum_s_s3.isnull()] = s3
A 6.0
B 3.0
C 0.0
D 508.0
E 404.0
F 300.0