7

I'm trying to drop all rows from this df where column 'DB Serial' contains the character *:

    DB Serial
0     13058
1     13069
2    *13070
3     13070
4     13044
5     13042

I am using:

df = df[~df['DB Serial'].str.contains('*')]

but i get this error:

    raise error, v # invalid expression
error: nothing to repeat
0

1 Answer 1

14

Escape * by \ because * is interpreted as regex:

'*' Causes the resulting RE to match 0 or more repetitions of the preceding RE

df = df[~df['DB Serial'].str.contains('\*')]
print (df)
  DB Serial
0     13058
1     13069
3     13070
4     13044
5     13042

If also get:

TypeError: bad operand type for unary ~: 'float'

then cast column to string, because mixed values - numeric with strings:

df = df[~df['DB Serial'].astype(str).str.contains('\*')]
print (df)
  DB Serial
0     13058
1     13069
3     13070
4     13044
5     13042

If possible NaNs values:

df = df[~df['DB Serial'].str.contains('\*', na=False)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.