4

I'm trying to remove any row that contains a "?" in a particular column.

I have this line:

df[~df.C.str.contains("?")]

which doesn't work. I get the following error:

error: nothing to repeat at position 0

However the following does work

df[~df.C.str.contains("abc")]

Does anyone know what it is about ? that stops it running?

4
  • df[~df.C.str.contains("?",regex=False)] ? Commented Jan 23, 2019 at 14:56
  • @BradSolomon - looking for better dupe Commented Jan 23, 2019 at 14:59
  • @BradSolomon check this link stackoverflow.com/questions/54219706/… Commented Jan 23, 2019 at 15:00
  • Yep - thats a better match, no pun intended @jezrael Commented Jan 23, 2019 at 15:01

1 Answer 1

4

.str.contains() expects a regular expression by default; ? is treated as a metacharacter, and using it alone will raise re.error.* Pass regex=False to search for a literal "?" character:

df[~df.C.str.contains("?", regex=False)]

* See re.compile("?")

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that works. Couldn't find an answer anywhere. I'll accept once timer expires

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.