Home > Blockchain >  Basic question on transforming object into float
Basic question on transforming object into float

Time:04-05

I am new at programming and I'm having this trouble:

"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"

I tried

y = pd.to_numeric(final_list[1])

and also

y = final_list[1].astype(float, errors = 'raise')

both say it is not possible because of "- "

What do I need to do? Transform "- " into NaN? How do I do that?

Thanks

CodePudding user response:

One of the solutions is to convert such non-convertible cases to NaN:

y = pd.to_numeric(final_list[1], errors='coerce')

  • Related