【发布时间】:2018-07-16 09:56:09
【问题描述】:
我正在处理具有以下形式的列的数据框:
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
请问我该如何解决这个问题?
【问题讨论】:
-
allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'], errors='coerce')工作怎么样? -
@jezrael 谢谢。它工作正常。但是,它仍然是一个对象。它没有被转换成浮点数,这使得该列不能用于其他目的
-
所以
print (allHoldingsFund['ratioBest'].dtype)再次返回object? -
是的
print (allHoldingsFund['ratioBest'].dtype) object我也可以从这个操作中看出哪个不起作用np.where(allHoldingsFund['ratioBest']>1,1,0) -
对我来说效果很好
标签: python python-3.x pandas numeric