pandas 类型错误:一元操作数类型错误~:浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52297740/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:01:44  来源:igfitidea点击:

TypeError: bad operand type for unary ~: float

pythonpandas

提问by fredley

df = df[~df["column"].str.contains("Total")]

TypeError: bad operand type for unary ~: 'float'

Why does .str.contains()return a float? What should I be doing here?

为什么.str.contains()返回一个浮点数?我应该在这里做什么?

回答by jezrael

I think there are NaNs values, so need specify parameter na:

我认为有NaNs 个值,因此需要指定参数na

df = pd.DataFrame({
    'column': ['Total','a',np.nan],
    'B': list(range(3))
})
print (df)
  column  B
0  Total  0
1      a  1
2    NaN  2

df = df[~df["column"].str.contains("Total", na=False)]
print (df)
  column  B
1      a  1
2    NaN  2