You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Series数据类型从object转为float时报错:无法将'no reading'转为浮点数

解决Series转float时因字符串值报错的问题

方案1:用pd.to_numeric处理错误值

pd.to_numeric支持错误处理参数,能把无法转换的值转为NaN,之后再转float:

import pandas as pd
values_int = pd.to_numeric(values, errors='coerce').astype(float)
  • errors='coerce'会将所有无法转为数值的内容替换为NaN,后续可通过fillna()方法填充这些缺失值(比如填充0、列均值等)。

方案2:先过滤无效值

如果不需要保留包含'no reading'的行,可以先筛选有效数据再转换:

valid_values = values[values != 'no reading']
values_int = valid_values.astype(float)

方案3:替换无效值后转换

把'no reading'替换成指定数值(比如0、列中位数)后再转类型:

values_replaced = values.replace('no reading', 0)  # 替换值可根据业务需求调整
values_int = values_replaced.astype(float)

内容的提问来源于stack exchange,提问作者MSH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 16:25:32