IT
파이썬 boolean indexing 과 논리 연산자
astrocker
2020. 12. 28. 23:54
반응형
라이브러리 호출 및 예제 작성
import pandas as pd # 데이터 처리용 라이브러리
import numpy as np # 수치해석용 라이브러리
sr=pd.Series([10,20,30,40,50],
index=['a','b','c','d','e'],
dtype=int,name='kor')
boolean
sr>=30
Result :
a False
b True
c True
d True
e True
Name: kor, dtype: bool
sr[sr>=30] # boolean indexing : 30 이상인 열만 출력
Result :
b 40
c 60
d 80
e 100
Name: kor, dtype: int64
논리 연산자 (and → & , or → | , not → ~) : pandas가 R의 파이썬 버전이라 R의 문법을 따름
sr[(sr==20)|(sr==40)] # 비트연산자를 논리연산자로 사용하므로 괄호로 우선 순위 조정
Result :
a 20
b 40
Name: kor, dtype: int64
sr[(sr>=20)&(sr<=40)]
Result :
a 20
b 40
Name: kor, dtype: int64
~((sr>=20)&(sr<=40))
Result :
a False
b False
c True
d True
e True
Name: kor, dtype: bool
sr[~((sr>=20)&(sr<=40))]
Result :
c 60
d 80
e 100
Name: kor, dtype: int64
예제)
# 급여에서 3.3% 세금을 제외한 지급액을 구하시오
salary=pd.Series([1000,2000,3000,4000,5000])
salary=salary*(1-0.033)
salary
Result :
0 967.0
1 1934.0
2 2901.0
3 3868.0
4 4835.0
dtype: float64
728x90
반응형