콘솔워크

dataframe contains and operation 여러 조건을 만족하는 데이터 필터링 본문

프로그래밍/python

dataframe contains and operation 여러 조건을 만족하는 데이터 필터링

이휘재123 2022. 10. 5. 09:02
반응형

데이터 프레임의 특정 열에 아래와 같은 문장이 들어있다.

"apple is delicious"
"banana is delicious"
"apple and banana both are delicious"

 

지금까지 사용했던 필터링 방식인 contains와 '|' 연산자를 사용한다면 아래와 같은 결과가 나온다.

df.col_name.str.contains("apple|banana")

# 결과
"apple is delicious",
"banana is delicious",
"apple and banana both are delicious"

 

이번 상황에서는 마지막줄에 있는 "apple and banana both are delicious"만을 필터링 하고 싶은데 이런 상황에서 사용 할 수 있는 몇가지 방법이 있다.

df[(df['col_name'].str.contains('apple')) & (df['col_name'].str.contains('banana'))]

# 결과
"apple and banana both are delicious"
df[df['col_name'].str.contains(r'^(?=.*apple)(?=.*banana)')]

# 결과
"apple and banana both are delicious"





# 위의 조건이 길어지면 가독성이 떨어지기 때문에 아래의 방법을 추천
base = r'^{}'
expr = '(?=.*{})'
words = ['apple', 'banana', 'cat']  # example
base.format(''.join(expr.format(w) for w in words))

# 결과
'^(?=.*apple)(?=.*banana)(?=.*cat)'

# base.format(''.join(expr.format(w) for w in words))을 위의 df조건에 넣으면 된다.

 

 

 

참고

https://stackoverflow.com/questions/37011734/pandas-dataframe-str-contains-and-operation

반응형