[Python] Pandas TypeError: Could not convert to numeric

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project where I imported data from SQL into a pandas DataFrame. This seems to go swimmingly, but when I take the pandas.mean() it throws a TypeError saying that a concatenated list of the values cannot be converted to numeric (see below):

    Example Dataframe:

    ProductSKU OverallHeight
    0 AAI2185 74.5
    1 AAI2275 47
    2 AAI2686 56.5
    3 AASA1002 73.23


    Function Call:

    avgValue = df["OverallHeight"].dropna().mean()


    Console Output:

    Traceback (most recent call last):
    File "C:\Program Files\Anaconda\lib\site-packages\pandas\core\generic.py", line 5310, in stat_func
    numeric_only=numeric_only)
    ...
    File "C:\Program Files\Anaconda\lib\site-packages\pandas\core\nanops.py", line 293, in nanmean
    the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))
    File "C:\Program Files\Anaconda\lib\site-packages\pandas\core\nanops.py", line 743, in _ensure_numeric
    raise TypeError('Could not convert %s to numeric' % str(x))
    TypeError: Could not convert 74.54756.573.23 to numeric


    The strangest thing is that it works perfectly fine when I import the same data through a CSV. It only breaks when I load it through SQL, could there be something I did incorrectly there?

    Continue reading...

Compartilhe esta Página