将对象转换为浮点数的基本问题
Basic question on transforming object into float
我是编程新手,遇到了这个问题:
y = pd.to_numeric(final_list\[1\])
Traceback (most recent call last):
File pandas_libs\lib.pyx:2315 in
pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string "- "
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
Input In [66] in <cell line: 1>
y = pd.to_numeric(final_list\[1\])
File
~\anaconda3\lib\site-packages\pandas\core\tools\numeric.py:184
in to_numeric values, _ = lib.maybe_convert_numeric(
File pandas_libs\lib.pyx:2357 in
pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string "- " at position 265066"
我试过了
y = pd.to_numeric(final_list\[1\])
还有
y = final_list\[1\].astype(float, errors = 'raise')
都说不可能,因为“-”
我需要做什么?将“-”转换为 NaN?我该怎么做?
谢谢
其中一个解决方案是将此类 non-convertible 情况转换为 NaN:
y = pd.to_numeric(final_list[1], errors='coerce')
我是编程新手,遇到了这个问题:
y = pd.to_numeric(final_list\[1\])
Traceback (most recent call last):
File pandas_libs\lib.pyx:2315 in pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string "- "
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
Input In [66] in <cell line: 1>
y = pd.to_numeric(final_list\[1\])
File ~\anaconda3\lib\site-packages\pandas\core\tools\numeric.py:184 in to_numeric values, _ = lib.maybe_convert_numeric(
File pandas_libs\lib.pyx:2357 in pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string "- " at position 265066"
我试过了
y = pd.to_numeric(final_list\[1\])
还有
y = final_list\[1\].astype(float, errors = 'raise')
都说不可能,因为“-”
我需要做什么?将“-”转换为 NaN?我该怎么做?
谢谢
其中一个解决方案是将此类 non-convertible 情况转换为 NaN:
y = pd.to_numeric(final_list[1], errors='coerce')