Pandas drop rows where column contains *
pandas, python
Solution
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 `NaN`s values:
df = df[~df['DB Serial'].str.contains('\*', na=False)]
Problem
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 ```