'Can anyone help and tell me why pd.to_numeric is throwing a valueerror?

clean_properties is the dataset, and Average_price is one of the columns. Its current dtype is object, and I need to convert it to a float. Everything I've looked at online says this is the correct format, but it throws a ValueError.

here is the .head() for the dataframe

London_Borough  ID  Month   Average_price
0   City of London  E09000001   1995-01-01  91448.98487
1   Barking & Dagenham  E09000002   1995-01-01  50460.2266
2   Barnet  E09000003   1995-01-01  93284.51832
3   Bexley  E09000004   1995-01-01  64958.09036
4   Brent   E09000005   1995-01-01  71306.56698

And this is what I'm trying that is throwing an error

clean_properties['Average_price'] = pd.to_numeric(clean_properties['Average_price'])

ValueError                                Traceback (most recent call last)
~\anaconda33\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()

ValueError: Unable to parse string "-"

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_30444/2671958707.py in <module>
      1 # Try this here
----> 2 clean_properties['Average_price'] = pd.to_numeric(clean_properties['Average_price'])

~\anaconda33\lib\site-packages\pandas\core\tools\numeric.py in to_numeric(arg, errors, downcast)
    181         coerce_numeric = errors not in ("ignore", "raise")
    182         try:
--> 183             values, _ = lib.maybe_convert_numeric(
    184                 values, set(), coerce_numeric=coerce_numeric
    185             )

~\anaconda33\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()

ValueError: Unable to parse string "-" at position 15264


Solution 1:[1]

Your StackTrace ends with: ValueError: Unable to parse string "-" at position 15264.

So probably Average_price column contains somewhere only "-", meaning actually "no data".

The indicated position (15264) is probably the row number, indicating the first place where the conversion failed.

One of possible solutions is to convert such inconvertible cases to NaN:

clean_properties['Average_price'] = pd.to_numeric(
    clean_properties['Average_price'], errors='coerce')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valdi_Bo