代码之家  ›  专栏  ›  技术社区  ›  SBad

对象未在pd.to\u numeric之后转换为numeric

  •  0
  • SBad  · 技术社区  · 6 年前

    我不明白为什么我的列allholdingsfund[‘RatioBest’]没有转换为数字?

    最初,我用数字0替换了空行,但int 0和floats数字并不能使列allholdingsfund['ratiobest']轻松转换为数字(得到未大小化对象的错误类型error:len())。现在,我留下了空行,并强制将浮点转换为数字,但它不起作用-allholdingsfund['ratiobest']仍然是一个对象。见下文

    allHoldingsFund['ratioBest']
    
    Out[170]: 
    100                          
    144                          
    187         0.520704845814978
    264                          
    386                          
    494                          
    597                          
    635         2.790492957746479
    
    allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'], errors='coerce')
    
    Name: ratioBest, Length: 1664, dtype: object
    

    谢谢你的帮助

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   jezrael    6 年前

    numpy.ndarray

    allHoldingsFund = pd.DataFrame({'ratioBest':[np.array(4.12), '']})
    print (allHoldingsFund)
            ratioBest
    0      4.12
    1    
    

    list 1d array

    arr = np.array(allHoldingsFund['ratioBest'].tolist())
    allHoldingsFund['ratioBest'] = (pd.to_numeric(arr, errors='coerce'))
    print (allHoldingsFund)
       ratioBest
    0       4.12
    1        NaN
    
    print (allHoldingsFund['ratioBest'].dtype)
    float64