python - 替换字符串时出现“AttributeError:'float'对象没有属性'replace'”错误
问题描述
我有一个列,其中包含文本。我必须替换(':','')。
当我运行此代码时:
df["text"] = [x.replace(':',' ') for x in df["text"]]
我正面临这个错误:
AttributeError: 'float' object has no attribute 'replace'
AttributeError Traceback (most recent call
last)
<ipython-input-13-3c116e6f21e2> in <module>
1 #df["text"] = df["text"].astype(str)
----> 2 df["text"] = [x.replace(':',' ') for x in df["text"]]
<ipython-input-13-3c116e6f21e2> in <listcomp>(.0)
1 #df["text"] = data["NOTES_ENT"].astype(str)
----> 2 df["text"] = [x.replace(':',' ') for x in df["text"]]
AttributeError: 'float' object has no attribute 'replace'
解决方案
错误很明显。您正在尝试替换浮点数中的字符。x 可能是一个浮点数。你可能想做 str(x) 这让你
f["text"] = [str(x).replace(':',' ') for x in df["text"]]
但我看不到浮点数包含 a 的情况:
推荐阅读
- python - InfluxDB 时间戳似乎没有使用提供的时间戳
- c# - 关于自适应卡片中的提交按钮
- sql-server - 如何为 ASP.NET Core Web 应用程序和 SQL Server 数据库创建目标 dacpac?
- python - Python 不会在子包中导入父包
- javascript - 全日历中的重复事件问题
- webpack - 为什么 less-loader 不起作用 TypeError: this.getOptions is not a function
- postgresql - 如何使用我需要在 Postgres plpgsql 过程中多次调用的 CTE 记录创建多个临时表?
- c# - 如何在 C# 中使用 Random 类对数组进行洗牌
- java - Java(在 Arraylist 中读取 userinput.equalsIgnoreCase())
- coldfusion - Coldfusion 2018 请求已超过允许的时间限制 标签:cfhttp