Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Pandas DataFrame row if contains character [duplicate]

Tags:

pandas

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?

like image 428
fred.schwartz Avatar asked Oct 28 '25 08:10

fred.schwartz


1 Answers

.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("?")

like image 113
Brad Solomon Avatar answered Oct 31 '25 05:10

Brad Solomon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!