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

后端 未结 3 1985
野的像风
野的像风 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:34

    You can use str.contains alone with a regex pattern using OR (|):

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

    Or you could add the series to a dataframe then use str.contains:

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

    Output:

    0 cat
    1 hat
    2 dog
    3 fog 
    

提交回复
热议问题