RL Researcher

03.판다스(Pandas) - Series 데이터 연산 본문

AI Basic/Pandas

03.판다스(Pandas) - Series 데이터 연산

Lass_os 2021. 1. 3. 23:47

1. Index를 기준으로 연산


 

s1 = pd.Series([1, 2, 3, 4], ['a', 'b', 'c', 'd'])
s1

========================================================================

<output>
a    1
b    2
c    3
d    4
dtype: int64


s2 = pd.Series([6, 3, 2, 1], ['d', 'c', 'b', 'a'])
s2

========================================================================

<output>
d    6
c    3
b    2
a    1
dtype: int64
s1 + s2

========================================================================

<output>
a     2
b     4
c     6
d    10
dtype: int64

2. 산술연산


  • Series의 경우에도 스칼라와의 연산은 각 원소별로 스칼라와의 연산이 적용

  • Series와의 연산은 각 인덱스에 맞는 값끼리 연산이 적용

    • 이때, 인덱스의 pair가 맞지 않으면, 결과는 NaN

s1 ** 2

========================================================================

<output>
a     1
b     4
c     9
d    16
dtype: int64
s1 ** s2

========================================================================

<output>
a       1
b       4
c      27
d    4096
dtype: int64

3. Index pair가 맞지 않는 경우


  • 해당 index에 대해선 NaN 값 생성

s1['k'] = 7
s1

========================================================================

<output>
a    1
b    2
c    3
d    4
k    7
dtype: int64


s2['e'] = 9
s2

========================================================================

<output>
d    6
c    3
b    2
a    1
e    9
dtype: int64
s1 + s2

========================================================================

<output>
a     2.0
b     4.0
c     6.0
d    10.0
e     NaN
k     NaN
dtype: float64
Comments