Home > other >  pandas constains with regular expression- special character and full word
pandas constains with regular expression- special character and full word

Time:02-01

I am trying to remove rows that contains any of this characters(@#% *=) and also a full word

col
ahoi*
word
be
df = df[~df[col].str.contains(r'[@#% *=](word))', regex=True)]

I achieved to remove special characters only with .str.contains(r'[@#% *=])', however I cannot remove the row with the full word.

What am I missing?

This is the expected result.

col
be

CodePudding user response:

IIUC, you need to use the or operator (|) instead of parenthesis :

df = df[~df["col"].str.contains(r'[@#% *=]|word', regex=True)]

​ Output :

print(df)
  col
2  be

CodePudding user response:

You can try:

>>> df[~df['col'].str.contains(r'(?:[@#% *=]|word)', regex=True)]
  col
2  be
  • Related