Home > Software engineering >  How to find multiple strings with str.contains in pandas
How to find multiple strings with str.contains in pandas

Time:04-02

I am trying to find out all rows which has "cov" in the column named hashtags of a dataset. I wanted to find the rows which contain "corona" too. How can I add additional parameter in str.contains()?

df=df[df["hashtags"].str.contains("cov",case=False)]     #wanted to add "corona" too as a parameter

df=df[text]

I tried to use OR operator(|) but it showed an error.

CodePudding user response:

df=df[df.hash_tags.str.contains('cov|corona',na=False)]

na=False means nan values if present will be evaluated to false

CodePudding user response:

Here is how the OR operator works with dataframe masking in Pandas:

df = df[
        (df['hash_tags'].str.contains('cov')) | 
        (df['hash_tags'].str.contains('corona'))
]

CodePudding user response:

You can use a single regex:

df = df[df['hash_tags'].str.contains('cov|corona', regex=True, case=False)]
  • Related