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

后端 未结 3 1960
野的像风
野的像风 2020-11-22 13:06

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 s

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 13:55

    One option is just to use the regex | character to try to match each of the substrings in the words in your Series s (still using str.contains).

    You can construct the regex by joining the words in searchfor with |:

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

    As @AndyHayden noted in the comments below, take care if your substrings have special characters such as $ and ^ which you want to match literally. These characters have specific meanings in the context of regular expressions and will affect the matching.

    You can make your list of substrings safer by escaping non-alphanumeric characters with re.escape:

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

    The strings with in this new list will match each character literally when used with str.contains.

提交回复
热议问题