본문 바로가기

코딩으로 익히는 Python/Pandas

[Python] 04. pandas Series 연산 : 산술관계논리(element wise),isin(),between()예제

728x90
반응형
SMALL
import pandas as pd
import numpy as np
data = {'aa':10,'bb':20,'cc':30,'dd':40, 'ee':50}
sr = pd.Series(data, name='국어점수')
sr
[OUT] :

aa    10
bb    20
cc    30
dd    40
ee    50
Name: 국어점수, dtype: int64

sr.index
[OUT] :

Index(['aa', 'bb', 'cc', 'dd', 'ee'], dtype='object')

산술관계논리(element wise)

sr+1
[OUT] :

aa    11
bb    21
cc    31
dd    41
ee    51
Name: 국어점수, dtype: int64

 

sr*2
[OUT] :

aa     20
bb     40
cc     60
dd     80
ee    100
Name: 국어점수, dtype: int64

 

sr>=30
[OUT] :

aa    False
bb    False
cc     True
dd     True
ee     True
Name: 국어점수, dtype: bool

 

sr[sr>30]
[OUT] :

dd    40
ee    50
Name: 국어점수, dtype: int64

 

sr[(sr==20) | (sr==40)]
[OUT] :

bb    20
dd    40
Name: 국어점수, dtype: int64

 

sr[(sr>=20) & (sr<=40)]
[OUT] :

bb    20
cc    30
dd    40
Name: 국어점수, dtype: int64

 

sr[~((sr>=20) & (sr<=40))]
[OUT] :

aa    10
ee    50
Name: 국어점수, dtype: int64

 

sr[sr.isin([20,40])]
[OUT] :

bb    20
dd    40
Name: 국어점수, dtype: int64

 

sr[~(sr.isin([20,40]))]
[OUT] :

aa    10
cc    30
ee    50
Name: 국어점수, dtype: int64
In [23]:

 

sr[sr.between(20,40)]
[OUT] :

bb    20
cc    30
dd    40
Name: 국어점수, dtype: int64

 

sr[sr.index == 'bb']
[OUT] :

bb    20
Name: 국어점수, dtype: int64

 

sr[sr.index.isin(['aa','cc'])]
[OUT] :

aa    10
cc    30
Name: 국어점수, dtype: int64

 


연습문제

# 다음 salary의 세금 3.3%를 제한 실수령액을 구하시오.

salary = pd.Series([1000,2000,3000,4000])

# solution

salary*(1-0.033)
[OUT] :

0     967.0
1    1934.0
2    2901.0
3    3868.0
dtype: float64

Review
- isin(), between()
728x90
반응형
LIST