[python] pandas에서 문자열에 목록의 하위 문자열 중 하나가 포함되어 있는지 테스트하는 방법은 무엇입니까?

df.isin()및 의 조합에 해당하는 기능이 df[col].str.contains()있습니까?

예를 들어, I has the series s = pd.Series(['cat','hat','dog','fog','pet']), I have to find all places where sof any of ['og', 'at'], I would want to get all than the
‘pet’.

나는 해결책이 있지만 다소 우아하지 않습니다.

searchfor = ['og', 'at']
found = [s.str.contains(x) for x in searchfor]
result = pd.DataFrame[found]
result.any()

이 작업을 수행하는 더 좋은 방법이 있습니까?



답변

한 가지 옵션은 정규식 |문자 를 사용 하여 시리즈의 단어에있는 각 하위 문자열을 일치시키는 것입니다 s(여전히 사용 str.contains).

다음 searchfor과 같이 단어를 결합하여 정규식을 구성 할 수 있습니다 |.

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

@AndyHayden 아래 코멘트에 언급 한 바와 같이 귀하의 문자열과 같은 특수 문자가있는 경우, 알아서 $하고 ^말 그대로 일치합니다. 이러한 문자는 정규식 컨텍스트에서 특정 의미를 가지며 일치에 영향을줍니다.

다음을 사용하여 영숫자가 아닌 문자를 이스케이프하여 하위 문자열 목록을 더 안전하게 만들 수 있습니다 re.escape.

>>> import re
>>> matches = ['$money', 'x^y']
>>> safe_matches = [re.escape(m) for m in matches]
>>> safe_matches
['\\$money', 'x\\^y']

이 새 목록에있는 문자열은와 함께 사용할 때 문자 그대로 각 문자와 일치합니다 str.contains.


답변

다음을 사용 str.contains하여 정규식 패턴과 함께 단독으로 사용할 수 있습니다 OR (|).

s[s.str.contains('og|at')]

또는 dataframe다음을 사용 하여 시리즈를 추가 할 수 있습니다 str.contains.

df = pd.DataFrame(s)
df[s.str.contains('og|at')] 

산출:

0 cat
1 hat
2 dog
3 fog 


답변

다음은 또한 작동하는 한 줄 람다입니다.

df["TrueFalse"] = df['col1'].apply(lambda x: 1 if any(i in x for i in searchfor) else 0)

입력:

searchfor = ['og', 'at']

df = pd.DataFrame([('cat', 1000.0), ('hat', 2000000.0), ('dog', 1000.0), ('fog', 330000.0),('pet', 330000.0)], columns=['col1', 'col2'])

   col1  col2
0   cat 1000.0
1   hat 2000000.0
2   dog 1000.0
3   fog 330000.0
4   pet 330000.0

Lambda 적용 :

df["TrueFalse"] = df['col1'].apply(lambda x: 1 if any(i in x for i in searchfor) else 0)

산출:

    col1    col2        TrueFalse
0   cat     1000.0      1
1   hat     2000000.0   1
2   dog     1000.0      1
3   fog     330000.0    1
4   pet     330000.0    0


답변