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

使用Python和Pandas遍历列替换值 逗号转小数点失效如何解决

问题根因
  • 你调用的Series.replace()默认是全值匹配规则,只有单元格内容完全等于,时才会触发替换,对类似123,45这种带逗号的数值字符串没有效果。
  • 当你设置inplace=True参数时,该方法会直接修改原对象,返回值为None,你同时将返回值赋值给列,就会导致整列被覆盖为None。
解决方法

直接使用Pandas字符串处理的向量化方法.str.replace(),该方法会匹配替换字符串内所有符合条件的字符,无需循环,性能更优:

# 定义需要处理的目标列
target_cols = ['Altitude Variation', 'Vehicle Speed Instantaneous']
# 批量替换列内逗号为点
opel_Df[target_cols] = opel_Df[target_cols].apply(lambda x: x.str.replace(',', '.'))

如果后续需要把这两列转换为数值类型,可以追加转换逻辑:

opel_Df[target_cols] = opel_Df[target_cols].astype(float)

如果偏好循环写法,可参考如下正确实现:

for col in ['Altitude Variation', 'Vehicle Speed Instantaneous']:
    opel_Df[col] = opel_Df[col].str.replace(',', '.')
    # 如需转数值放开下一行注释
    # opel_Df[col] = opel_Df[col].astype(float)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 14:54:03