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
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
One of the fastest ways to delete rows that contain a specific value or fulfill a given condition is to filter these. Once you have the filtered data, you can delete all these rows (while the remaining rows remain intact).
Pandas provide data analysts a way to delete and filter data frame using . drop() method. Rows can be removed using index label or column name using this method.
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With