Python 对象在pd.to\u numeric之后未转换为numeric
我不明白为什么我的列AllHoldingFund['ratioBest']没有转换成数字 最初,我用数字0替换了空行,但int 0和floats数字并没有使列allHoldingsFund['ratioBest']容易转换为数值(得到错误类型错误:unsized对象的len()。现在,我保留了空行,并强制将浮动转换为数字,但这不起作用-AllHoldingFund['ratioBest']仍然是一个对象。见下文Python 对象在pd.to\u numeric之后未转换为numeric,python,pandas,numeric,Python,Pandas,Numeric,我不明白为什么我的列AllHoldingFund['ratioBest']没有转换成数字 最初,我用数字0替换了空行,但int 0和floats数字并没有使列allHoldingsFund['ratioBest']容易转换为数值(得到错误类型错误:unsized对象的len()。现在,我保留了空行,并强制将浮动转换为数字,但这不起作用-AllHoldingFund['ratioBest']仍然是一个对象。见下文 allHoldingsFund['ratioBest'] Out[170]: 1
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
谢谢你的帮助
谢谢我明白你的问题-值不是标量,而是
numpy.ndarray
s:
allHoldingsFund = pd.DataFrame({'ratioBest':[np.array(4.12), '']})
print (allHoldingsFund)
ratioBest
0 4.12
1
解决方案转换为列表
,然后转换为1d数组
:
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
想法-数据是保密的吗?你的熊猫版本是什么?是的,数据是保密的。你有什么想法?pd.\uuuu version\uuuu Out[171]:“0.22.0”如果没有数据,很难复制您的问题。在我的一个小示例中,您的代码可以正常工作。感谢您在这方面的持续帮助。我得到的错误是:TypeError:不知道如何将标量数转换为float@SBad-我发现问题并添加解决方案。