简体   繁体   中英

Pandas drop rows where column contains *

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM