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

如何获取DataFrame特定列精确最大值?解决浮点数精度问题

问题:获取DataFrame列的精确最大值

我想获取DataFrame中Climate change (kg CO2 eq.)列的精确最大值(实际应为1),但执行scaled_df["Climate change (kg CO2 eq.)"].max()得到的结果是0.9999999999999999,该如何得到精确数值?注:数据集很大,仅提供片段如下:

Part's Orientation (Support's volume) (cm^3)    Climate change (kg CO2 eq.) Climate change, incl biogenic carbon (kg CO2 eq.)   Fine Particulate Matter Formation (kg PM2.5 eq.)    Fossil depletion (kg oil eq.)   Freshwater Consumption (m^3)    Freshwater ecotoxicity (kg 1,4-DB eq.)  Freshwater Eutrophication (kg P eq.)    Human toxicity, cancer (kg 1,4-DB eq.)  Human toxicity, non-cancer (kg 1,4-DB eq.)  Ionizing Radiation (Bq. C-60 eq. to air)    Land use (Annual crop eq. yr)   Marine ecotoxicity (kg 1,4-DB eq.)  Marine Eutrophication (kg N eq.)    Metal depletion (kg Cu eq.) Photochemical Ozone Formation, Ecosystem (kg NOx eq.)   Photochemical Ozone Formation, Human Health (kg NOx eq.)    Stratospheric Ozone Depletion (kg CFC-11 eq.)   Terrestrial Acidification (kg SO2 eq.)  Terrestrial ecotoxicity (kg 1,4-DB eq.)
0   0.210866    0.040430    1.0 0.0 0.00    0.666667    0.040088    0.063802    0.040013    0.083205    0.005648    0.113808    0.104798    0.086400    0.108284    0.007368    0.091120    0.108676    0.090401    0.087426    0.101706    0.079028    0.080495    0.078380    0.082404    0.029502
1   0.210866    0.040430    1.0 0.2 0.00    0.666667    0.036597    0.038086    0.016068    0.074884    0.002636    0.045640    0.102285    0.082884    0.043371    0.003107    0.086700    0.105749    0.087161    0.084130    0.048885    0.072878    0.073529    0.074829    0.075438    0.011870
2   0.210866    0.044796    1.0 0.4 0.00    0.666667    0.031013    0.030436    0.008507    0.073035    0.001883    0.023401    0.102914    0.082494    0.022264    0.001854    0.086279    0.105749    0.086937    0.084130    0.032152    0.071341    0.071981    0.074698    0.073447    0.006456
3   0.210866    0.044311    1.0 0.6 0.00    0.666667    0.031013    0.026693    0.004883    0.072111    0.001506    0.012936    0.102914    0.082103    0.012289    0.001103    0.086069    0.105423    0.086602    0.084130    0.023950    0.070572    0.071207    0.074435    0.072452    0.003748
4   0.210866    0.045281    1.0 1.0 0.00    0.666667    0.031711    0.023438    0.001260    0.071803    0.001883    0.002180    0.103542    0.082884    0.002024    0.000601    0.086490    0.106074    0.087049    0.084542    0.015748    0.070572    0.071207    0.074961    0.072452    0.001249

解决方案

  • 四舍五入到合理精度后取最大值
    针对浮点数精度误差,先对列值进行四舍五入,再计算最大值:

    import numpy as np
    scaled_df["Climate change (kg CO2 eq.)"].apply(lambda x: np.round(x, 6)).max()
    

    这里的6可根据数据实际精度调整,确保覆盖真实值的小数位数。

  • 直接匹配近似最大值的真实值
    已知真实最大值为1,可通过筛选接近1的值来获取精确值:

    # 方法A:阈值筛选
    scaled_df[scaled_df["Climate change (kg CO2 eq.)"] >= 0.999999]["Climate change (kg CO2 eq.)"].iloc[0]
    
    # 方法B:使用numpy的isclose函数匹配
    import numpy as np
    target_col = scaled_df["Climate change (kg CO2 eq.)"]
    exact_max = target_col[np.isclose(target_col, 1)].iloc[0]
    
  • 转换为高精度Decimal类型计算
    若需要彻底避免浮点数精度问题,可将列转换为Decimal类型后计算最大值:

    from decimal import Decimal
    target_col = scaled_df["Climate change (kg CO2 eq.)"].apply(Decimal)
    exact_max = target_col.max()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 23:31:19