簡體   English   中英

計算總體標准差時,錯誤無法將類型“str”轉換為分子/分母

[英]Error can't convert type 'str' to numerator/denominator while calculating population standard deviation

我正在嘗試計算 python 中我的數據框的總體標准差。 我通過導入statistics庫來使用 function statistics.pstdev(df) 我已經從我的數據集中排除了所有非浮點列,它只包含浮點數據列。 但是我在執行時收到以下錯誤:

TypeError                                 Traceback (most recent call last)
<ipython-input-118-f8b494b6fd29> in <module>()
      1 import statistics
----> 2 statistics.pstdev(tot2)

5 frames
/usr/lib/python3.6/statistics.py in pstdev(data, mu)
    664 
    665     """
--> 666     var = pvariance(data, mu)
    667     try:
    668         return var.sqrt()

/usr/lib/python3.6/statistics.py in pvariance(data, mu)
    635     if n < 1:
    636         raise StatisticsError('pvariance requires at least one data point')
--> 637     T, ss = _ss(data, mu)
    638     return _convert(ss/n, T)
    639 

/usr/lib/python3.6/statistics.py in _ss(data, c)
    533     """
    534     if c is None:
--> 535         c = mean(data)
    536     T, total, count = _sum((x-c)**2 for x in data)
    537     # The following sum should mathematically equal zero, but due to rounding

/usr/lib/python3.6/statistics.py in mean(data)
    310     if n < 1:
    311         raise StatisticsError('mean requires at least one data point')
--> 312     T, total, count = _sum(data)
    313     assert count == n
    314     return _convert(total/n, T)

/usr/lib/python3.6/statistics.py in _sum(data, start)
    146     for typ, values in groupby(data, type):
    147         T = _coerce(T, typ)  # or raise TypeError
--> 148         for n,d in map(_exact_ratio, values):
    149             count += 1
    150             partials[d] = partials_get(d, 0) + n

/usr/lib/python3.6/statistics.py in _exact_ratio(x)
    228         return (x, None)
    229     msg = "can't convert type '{}' to numerator/denominator"
--> 230     raise TypeError(msg.format(type(x).__name__))
    231 
    232 

TypeError: can't convert type 'str' to numerator/denominator

您應該將 dataframe 的一列傳遞給statistics.pstdev ,而不是整個 dataframe。

當將 dataframe 視為可迭代時,這將導致列名(它們是字符串),而不是列的值。 這就是您收到此錯誤的原因。

因此,假設您要計算名為"values"的列的標准差,您應該使用

statistics.pstdev(tot2["values"])

或者,只需使用DataFrame.std方法一次獲取所有列的標准差:

tot2.std(axis=0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM