Pandas Series는 다양한 연산을 지원합니다. 이들 연산은 요소별로 수행되며, 색인이 같은 요소끼리 연산이 수행됩니다. 이들 연산은 산술 연산, 비교 연산, 논리 연산, 집계 연산 등이 있습니다.
산술 연산은 기본적인 산술 연산(+, -, *, /)과 지수 연산(**)을 지원합니다.
아래의 코드는 Pandas Series의 산술 연산을 수행하는 예입니다.
import pandas as pd
s1 = pd.Series([1, 3, 5, np.nan, 6, 8], index=['a', 'b', 'c', 'd', 'e', 'f'])
s2 = pd.Series([2, 4, 6, 8, 10, 12], index=['a', 'b', 'c', 'd', 'e', 'f'])
# 산술 연산
print(s1 + s2) # a 3.0
# b 7.0
# c 11.0
# d NaN
# e 16.0
# f 20.0
# dtype: float64
print(s1 - s2) # a -1.0
# b -1.0
# c -1.0
# d NaN
# e -4.0
# f -4.0
# dtype: float64
print(s1 * s2) # a 2.0
# b 12.0
# c 30.0
# d NaN
# e 60.0
# f 96.0
# dtype: float64
print(s1 / s2) # a 0.5
# b 0.75
# c 0.83
# d NaN
# e 0.60
# f 0.67
# dtype: float64
Pandas Series는 비교 연산을 지원합니다. 비교 연산은 기본적인 비교 연산(>, <, >=, <=, ==, !=)과 논리 연산(&, |)을 지원합니다.
아래의 코드는 Pandas Series의 비교 연산을 수행하는 예입니다.
import pandas as pd
s1 = pd.Series([1, 3, 5, np.nan, 6, 8], index=['a', 'b', 'c', 'd', 'e', 'f'])
s2 = pd.Series([2, 4, 6, 8, 10, 12], index=['a', 'b', 'c', 'd', 'e', 'f'])
# 비교 연산
print(s1 > s2) # a False
# b False
# c False
# d NaN
# e False
# f False
# dtype: bool
print(s1 < s2) # a True
# b True
# c True
# d NaN
# e True
# f True
# dtype: bool
print(s1 >= s2) # a False
# b False
# c False
# d NaN
# e False
# f False
# dtype: bool
print(s1 <= s2) # a True
# b True
# c True
# d NaN
# e True
# f True
# dtype: bool
print(s1 == s2) # a False
# b False
# c False
# d NaN
# e False
# f False
# dtype: bool
print(s1 != s2) # a True
# b True
# c True
# d NaN
# e True
# f True
# dtype: bool
# 논리 연산
print((s1 > 2) & (s2 < 10)) # a False
# b True
# c True
# d NaN
# e True
# f True
# dtype: bool
2022.08.03 - [프로그래밍/파이썬(Python)] - 파이썬(Python) Pandas Series
파이썬(Python) Pandas Series
Series Series는 파이썬 Pandas의 자료 구조 중 하나이다. 코드 import pandas as pd import numpy as np pd.__version__ #Pandas version 확인 s = pd.Series([0, 1, 2, 3, 4, 5]) print(s) 현재 사용중인 Pandas 의 버전은 version 명령으
com-flex.tistory.com
2022.12.30 - [프로그래밍/파이썬(Python)] - 파이썬(Python) Pandas Series index
파이썬(Python) Pandas Series index
Pandas Series는 색인(index)를 이용해서 각 요소에 접근할 수 있습니다. 색인은 문자열 값이나 정수 값을 사용할 수 있으며, 정수 값을 이용한 색인은 NumPy 배열과 유사합니다. 아래의 코드는 Pandas Seri
com-flex.tistory.com
'프로그래밍 > 파이썬(Python)' 카테고리의 다른 글
파이썬(Python) 문자열 다루기 (0) | 2022.12.30 |
---|---|
파이썬(Python) Pandas Series, Dataframe 관계 (0) | 2022.12.30 |
파이썬(Python) Pandas Series, Dataframe index #1 (0) | 2022.12.30 |
셀레니움(Selenium) (2) | 2022.12.28 |
파이썬(Python) 크롤링(Crawling) (2) | 2022.12.28 |