我正在处理具有以下形式的列的数据框:
allHoldingsFund ['ratioBest']
Out[72]:
65357 0.0
65371 0.0
65394 2.396777442094666
65397 0.0
65433 0.0167993412023058
65462 0.0
65560 0.0
Name: ratioBest, Length: 1664, dtype: object
该列是一个对象,我通常使用allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'])
但是,当我这样做时,出现一个我无法解决的错误:
pd.to_numeric(allHoldingsFund['ratioBest'])
Traceback (most recent call last):
File "/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-71-6f0ccaf63f24>", line 1, in <module>
pd.to_numeric(allHoldingsFund['ratioBest'])
File "/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/pandas/core/tools/numeric.py", line 133, in to_numeric
coerce_numeric=coerce_numeric)
File "pandas/_libs/src/inference.pyx", line 1111, in pandas._libs.lib.maybe_convert_numeric
TypeError: len() of unsized object
请问如何解决此问题?
答案 0 :(得分:0)
对我来说,很好的参数errros='coerce'
用于将没有数值转换为NaN
s:
allHoldingsFund = pd.DataFrame({'ratioBest':[['123'],'123','aaa']})
allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'], errors='coerce')
print (allHoldingsFund)
ratioBest
0 NaN
1 123.0
2 NaN
print (allHoldingsFund['ratioBest'].dtypes)
float64
答案 1 :(得分:0)
函数to_numeric
将参数转换为数字类型。
仅当计算类似len() of unsized object
的值len()
时,您会得到int
的错误。因此,对您的错误的唯一解释是您存储该列的任何数据结构,其中的一部分或全部已经以float
的形式存储,因为熊猫正试图找到其长度并得到上述错误。