본문 바로가기
목차
Python/pandas

[Pandas] isin() 메서드: 가독성 높은 boolean mask 만들기.

by ds31x 2025. 8. 28.
728x90
반응형

https://datascienceparichay.com/article/pandas-check-if-column-contains-string-from-list/

isin() 메서드란?

isin() 메서드는
SeriesDataFrame의 각 element(or item)가 지정된 값 목록에 포함되어 있는지 확인하여
boolean mask를 반환하는 메서드임

  • 반환된 boolean mask는 조건부 필터링이나 데이터 선택에 바로 활용가능함.
  • df[df['column'].isin([1, 2, 3])]처럼 대괄호 인덱싱과 함께 사용하여 특정 값들만 포함된 행을 추출하는 용도로 자주 사용됨.

특히,
여러 값에 대한 OR 조건을 간단하게 표현할 수 있어
== 연산자를 반복 사용하는 종래의 condition 기반 boolean mask보다 가독성이 높음.

2025.08.28 - [Python/pandas] - [Pandas] Boolean Mask 와 where()/mask()

 

[Pandas] Boolean Mask 와 where()/mask()

Boolean Mask란:Boolean mask는 True 또는 False로 구성(=boolean)된 시퀀스(Series/DataFrame/ndarray/list) 객체를 이용하여 Pandas에서 특정 데이터를 선택하는 등의 마스킹(masking)을 하는 것을 가리킴.사용방식:Series

ds31x.tistory.com

 

isin() 메서드는 pandas의 Series와 DataFrame 모두에서 지원.


주요 특징:

  • isin()메서드는 모두 동일한 shape의 불린(Boolean) 객체로 구성된 Series/DataFrame 을 반환
  • 리스트, 튜플, Series, DataFrame 등을 값으로 전달할 수 있음.
  • `DataFrame의 경우 각 셀별로 해당 값이 포함되어 있는지 확인

ex0: Series.isin()

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5])
result = s.isin([2, 4, 6])
print(result)
# 0    False
# 1     True
# 2    False  
# 3     True
# 4    False

DataFrame.isin()

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})
result = df.isin([1, 'y'])
print(result)
#        A      B
# 0   True  False
# 1  False   True
# 2  False  False
728x90