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

Python中DataFrame列四舍五入报错:float类型无rint方法问题

解决DataFrame列保留小数时的loop of ufunc does not support argument 0 of type float which has no callable rint method错误

这个报错的核心原因是Einkommen列的数据类型不符合要求——要么是object类型(混有非数值内容),要么是非常规的float类型,导致round()方法无法正常调用底层数值处理函数。以下是具体解决步骤:

  • 第一步:检查并转换列的数据类型
    先确认列的类型:

    print(scatterLabelsEinkommen['Einkommen'].dtype)
    

    如果输出是object,说明列中存在无法直接识别为数值的内容,先用pd.to_numeric强制转换为数值型,同时处理无效值:

    import pandas as pd
    scatterLabelsEinkommen['Einkommen'] = pd.to_numeric(scatterLabelsEinkommen['Einkommen'], errors='coerce')
    

    参数errors='coerce'会把无法转换的内容转为NaN,后续你可以根据需求用fillna()填充或dropna()删除这些行。

  • 第二步:执行保留小数操作
    转换为数值型后,再调用round()方法(注意你需要保留1位小数,所以decimals=1):

    scatterLabelsEinkommen['Einkommen'] = scatterLabelsEinkommen['Einkommen'].round(decimals=1)
    
  • 备选方案:直接强制转换为标准float类型
    如果转换为数值型后仍报错,可能是列中存在自定义float对象,直接转为标准float再处理:

    scatterLabelsEinkommen['Einkommen'] = scatterLabelsEinkommen['Einkommen'].astype(float).round(decimals=1)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 01:31:03