본문 바로가기
Development/Python

[Python] Pandas DataFrame 혹은 Series에서 문자열 조건 결과 얻기(str.contains)

by 성딱이 2022. 2. 9.
반응형
방법
<Series>.str.contains('A|B')
<DataFrame['colA']>.str.contains('A|B')

 

예시
>>> series = pd.Series(['cat','hat','dog','fog','pet'])
>>> searchfor = ['og', 'at']

>>> series[series.str.contains('|'.join(searchfor))]
>>> series[series.str.contains('og|at')]
0    cat
1    hat
2    dog
3    fog
dtype: object

 

 

출처 : https://stackoverflow.com/questions/26577516/how-to-test-if-a-string-contains-one-of-the-substrings-in-a-list-in-pandas

 

How to test if a string contains one of the substrings in a list, in pandas?

Is there any function that would be the equivalent of a combination of df.isin() and df[col].str.contains()? For example, say I have the series s = pd.Series(['cat','hat','dog','fog','pet']), and I

stackoverflow.com

 

 

반응형

댓글