python - 将列从对象转换为浮点数时出现值错误
问题描述
我正在尝试将列“value $”从数据类型“Object”转换为“float”,因为该列将涉及数值计算。
我最初使用以下方法替换了本专栏中的“$”:
df['Value $'] = df['Value $'].replace({'\$': ''},regex=True)
然后使用以下方法将其转换为数字:
df['Value $'] = df['Value $'].astype(dtype=np.float64)
解决方案
可能是因为您也应该替换逗号:
df['Value $'] = df['Value $'].replace({'\$|,': ''}, regex=True)
df['Value $'] = df['Value $'].astype(dtype=np.float64)
推荐阅读
- xml - 如何使用 XSLT 制作下拉列表?
- html - 为什么在主页.html 中的 JATE2.0 Solr-7.2.1 中连接被拒绝?
- android - 父行的颤振列自动高度
- ibeacon-android - 关于 AltBeacon 库,didRangeBeaconsInRegion() 是否不受 Android O 后台限制的影响?
- python - 在对值进行一些计算后从现有字典创建新字典
- angularjs - Chrome 在网页中显示“加载 PDF 时出错”
- r - 忽略 period.apply 上的 NA
- msbi - 将字段值从子报表传递到 SSRS 2013 中的主报表
- generics - 如何在具有生命周期的特征上创建结构泛型?
- javascript - 在for循环之后同步运行Node js程序