Pandas.Series (1D) - 구조

JU_Pakk·2021년 12월 29일
0

Pandas

목록 보기
2/5

★ Series (1D)

✓ 1차원의 labeled array이다.
✓ can holding any data type(integers, strings, floating point numbers, python objects, etc.)
✓ axis label은 index 로 나타낸다.
✓ 기본 구조 >>> s = pd.Series(data,index=index)
s = pd.Series(,name = 'something') >> 이걸로 object 생성 가능 (s1 = s.rename('another')으로 해서 다른 object 생성 가능)
✓ data는 1.Python dict 2.ndarray 3.scalar value등으로 이뤄진다.

1. Python dict 예시

✓ index가 pass되면, 그에 맞는 value도 pull된다

In[] : dict = {"b":1,"a":0,"c":2}
In[] : pd.Series(dict)
Out[] : 
b 1
a 0
c 2
dtype: int64
In[] : pd.Series(dict, index=["b","a","d","c"])
Out[] : 
b 1
a 0
d NaN
c 2
dtype: int64
s["a"]
"e" in s
s["f"]
s.get("f")

2. ndarray 예시

✓ data의 길이와 index의 개수가 같아야 한다(ex)s = pd.Series(np.random.randn(5), index={["a","b","c","d","e"]}
✓ ndarray와 비슷하게 작동됨

s[0] 
s[:3]
s[s > s.median()]
s[[4,3,1]] >>> indexing 부분 더 보기
np.exp(s)

✓ Series가 ndarray-like일때, 1.actual array를 얻으려면

# 1.actual array 얻으려면
s.array
# 2.actual ndarray 얻으려면
s.to_numpy()

3. scalar value 예시

pd.Series(5.0, index=["a","b","c","d","e"])

000. Thinking

✓ Series와 ndarray의 key difference (Series는 automatically align the data based on label)

Input[]: s[1:] + s[:-1]
Output[]:
a	NaN
b	-0.565727
c	-3.018117
d	-2.271265
e	NaN
profile
JU_PAKK

0개의 댓글