티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Filter pandas DataFrame by substring criteria

부분 문자열 기준으로 판다스 데이터프레임 필터링

 문제 내용 

I have a pandas DataFrame with a column of string values. I need to select rows based on partial string matches.

저는 문자열 값을 열로 갖고 있는 판다스 데이터프레임을 가지고 있습니다. 부분 문자열 일치를 기준으로 행을 선택해야 합니다.

 

Something like this idiom:

이 관용구와 비슷한 것:
re.search(pattern, cell_in_question) 

 

returning a boolean. I am familiar with the syntax of df[df['A'] == "hello world"] but can't seem to find a way to do the same with a partial string match, say 'hello'.

부울을 반환합니다. df[df['A'] == "hello world"] 구문에 익숙하지만 'hello'와 같이 부분 문자열 일치로 동일한 작업을 수행하는 방법을 찾지 못 했습니다.

 

 

 

 높은 점수를 받은 Solution 

Vectorized string methods (i.e. Series.str) let you do the following:

벡터화된 문자열 메소드(즉, Series.str)를 사용하면 다음을 수행할 수 있습니다.
df[df['A'].str.contains("hello")]

 

This is available in pandas 0.8.1 and up.

이것은 0.8.1 이상의 판다에서 이용할 수 있다.

 

 

 

 가장 최근 달린 Solution 

Somewhat similar to @cs95's answer, but here you don't need to specify an engine:

@cs95의 답변과 다소 유사하지만 여기서 엔진을 지정할 필요는 없습니다.
df.query('A.str.contains("hello").values')

 

 

출처 : https://stackoverflow.com/questions/11350770/filter-pandas-dataframe-by-substring-criteria

반응형
댓글
공지사항
최근에 올라온 글