Pandas.mean()TypeError:无法转换为数字

时间:2017-06-13 13:18:47

标签: python pandas numpy

我正在开发一个项目,我将数据从SQL导入到pandas DataFrame中。这似乎是游泳,但是当我接受pandas.mean()时,它会抛出一个TypeError,说明一个值的连接列表无法转换为数字(见下文):

示例数据帧:

df =
  ProductSKU OverallHeight-ToptoBottom
0   AAI2185                      74.5
1   AAI2275                        47
2   AAI2686                      56.5
3  AASA1002                     73.23
4  AASA1032                     39.37
5  AASA1039                     72.44
6  AASA1099                      75.6
7  AASA1101                        38
8  ABCM1910                        69
9  ABCM1980                        72

功能调用:

def summarizeTagData(df, tag):
    avgValue = df.loc[:,tag].dropna().mean()             <--- Breaks here
    stdevValue = df.loc[:,tag].dropna().std()
    lowerBound = max(avgValue-(3*stdevValue),0)
    upperBound = (avgValue+(3*stdevValue))
    outsideRangeCount = df[df[tag]>upperBound].shape[0]
    missingDataCount = df[df[tag].isnull()].shape[0]
    dataDict = {"Average":avgValue
            , "StDev":stdevValue
            , "UpperBound":upperBound
            , "LowerBound":lowerBound
            , "OutsideRange":outsideRangeCount
            , "MissingData":missingDataCount
            }
    return dataDict

控制台输出:

summarizeTagData(df, 'OverallHeight-ToptoBottom')
    Traceback (most recent call last):

  File "<ipython-input-22-f1f26a0a0520>", line 1, in <module>
    summarizeTagData(df, 'OverallHeight-ToptoBottom')

  File "C:/Users/tmori/Google Drive/Projects/Product Dimension Accuracy/ProductDataTag_Analysis.py", line 23, in summarizeTagData
    avgValue = df.loc[:,tag].dropna().mean()

  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.2339.3772.4475.6386972 to numeric

最奇怪的事情(以及我无法弄清楚的)是,当我通过CSV导入相同的数据时,它完全正常。它只会在我通过SQL加载时中断,是否有我在那里做错的东西?

最佳, 汤姆

1 个答案:

答案 0 :(得分:0)

如控制台输出所示,数据框列 'OverallHeight-ToptoBottom' 存在问题。

作为@Warren Weckesser commented,我的猜测是列包含字符串。为了检查该列的数据类型运行

print(df['OverallHeight-ToptoBottom'].dtype) 

假设上述情况成立,将列数据类型转换为float应该可以解决问题。为此使用 pandas.to_numeric

df["OverallHeight-ToptoBottom"] = pd.to_numeric(df["OverallHeight-ToptoBottom"], downcast="float")