series
data로만 생성하기
import numpy as np
import pandas as pd
s1 = pd.Series([1, 2, 3])
s1
>>> 0 1
1 2
2 3
dtype: int64
s2 = pd.Series(['a', 'b', 'c'])
s2
>>> 0 a
1 b
2 c
dtype: object
s3 = pd.Series(np.arange(200))
s3
>>> 0 0
1 1
2 2
3 3
4 4
...
195 195
196 196
197 197
198 198
199 199
Length: 200, dtype: int32
data, index함께 명시하기
s4 = pd.Series([1, 2, 3], [100, 200, 300])
s4
>>> 100 1
200 2
300 3
dtype: int64
s5 = pd.Series([1, 2, 3], ['a', 'm', 'k'])
s5
>>> a 1
m 2
k 3
dtype: int64
data, index, data type 함께 명시하기
s6 = pd.Series(np.arange(5), np.arange(100, 105), dtype=np.int16)
s6
>>> 100 0
101 1
102 2
103 3
104 4
dtype: int16
인덱스 활용하기
s6.index
>>> Int64Index([100, 101, 102, 103, 104], dtype='int64')
s6.values
>>> array([0, 1, 2, 3, 4], dtype=int16)
s6[104]
>>> 4
s6[104] = 70
s6
>>> 100 0
101 1
102 2
103 3
104 70
dtype: int16
s6[105] = 90
s6[200] = 80
s6
>>> 100 0
101 1
102 2
103 3
104 70
105 90
200 80
dtype: int64
s7 = pd.Series(np.arange(7), s6.index)
s7
>>> 100 0
101 1
102 2
103 3
104 4
105 5
200 6
dtype: int32
Series size, shape, unique, count, value_counts 함수
s = pd.Series([1, 1, 2, 1, 2, 2, 2, 1, 1, 3, 3, 4, 5, 5, 7, np.NaN])
s
>>> 0 1.0
1 1.0
2 2.0
3 1.0
4 2.0
5 2.0
6 2.0
7 1.0
8 1.0
9 3.0
10 3.0
11 4.0
12 5.0
13 5.0
14 7.0
15 NaN
dtype: float64
len(s)
>>> 16
s.size
>>> 16
s.shape
>>> (16,)
s.unique()
>>> array([ 1., 2., 3., 4., 5., 7., nan])
s.count()
>>> 15
a = np.array([2, 2, 2, 2, np.NaN])
a.mean()
>>> nan
b = pd.Series(a)
b.mean()
>>> 2.0
s.mean()
>>> 2.6666666666666665
s.value_counts()
>>> 1.0 5
2.0 4
5.0 2
3.0 2
7.0 1
4.0 1
dtype: int64
index를 활용하여 멀티플한 값에 접근
s[[5, 7, 8, 10]].value_counts()
>>> 1.0 2
3.0 1
2.0 1
dtype: int64
head, tail 함수
s.head(n=7)
>>> 0 1.0
1 1.0
2 2.0
3 1.0
4 2.0
5 2.0
6 2.0
dtype: float64
s.tail()
>>> 11 4.0
12 5.0
13 5.0
14 7.0
15 NaN
dtype: float64
머신러닝과 데이터 분석 A-Z 올인원 패키지 Online. 👉 https://bit.ly/3cB3C8y