Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas drop rows where column contains *

Tags:

python

pandas

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
like image 905
warrenfitzhenry Avatar asked Apr 23 '17 08:04

warrenfitzhenry


People also ask

How do I drop rows in pandas based on column condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do you drop a row with a certain value?

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).

How do you drop a set of rows in pandas?

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.


1 Answers

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)]
like image 145
jezrael Avatar answered Sep 28 '22 16:09

jezrael