Pandas 删除列包含 * 的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43568760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas drop rows where column contains *
提问by warrenfitzhenry
I'm trying to drop all rows from this df where column 'DB Serial' contains the character *:
我试图从这个 df 中删除所有行,其中“DB Serial”列包含字符 *:
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
回答by jezrael
Escape *
by \
because *
is interpreted as regex:
Escape *
by\
因为*
被解释为正则表达式:
'*'Causes the resulting RE to match 0 or more repetitions of the preceding RE
'*'导致结果 RE 匹配前面 RE 的 0 次或多次重复
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'
类型错误:一元的错误操作数类型〜:'float'
then cast column to string
, because mixed values - numeric with strings:
然后将列转换为string
,因为混合值 - 带字符串的数字:
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:
如果可能,NaN
s 值:
df = df[~df['DB Serial'].str.contains('\*', na=False)]