2017-02-26 31 views
1

我正在嘗試計算列上的組的中值。我發現了一個很明顯的例子,在熊貓計算列上的組的中值

Pandas: Calculate Median of Group over Columns

這個問題和答案是正是我所需要的答案。我創建貼通過細節上的工作確切比如我自己

import pandas 
import numpy 

data_3 = [2,3,4,5,4,2] 
data_4 = [0,1,2,3,4,2] 

df = pandas.DataFrame({'COL1': ['A','A','A','A','B','B'], 
         'COL2': ['AA','AA','BB','BB','BB','BB'], 
         'COL3': data_3, 
         'COL4': data_4}) 

m = df.groupby(['COL1', 'COL2'])[['COL3','COL4']].apply(numpy.median) 

當我嘗試了我遇到的錯誤

TypeError: Series.name must be a hashable type 

列計算組的中位數。如果我做同樣的代碼唯一的區別是用不同的統計數據(平均數,最小數,最大數,標準差)代替中位數,並且一切正常。

我不明白這個錯誤的原因,爲什麼它只出現在中位數,這是我真正需要計算。

預先感謝您的幫助,

鮑勃

以下是完整的錯誤消息。我正在使用python 3.5.2

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-12-af0ef7da3347> in <module>() 
----> 1 m = df.groupby(['COL1', 'COL2'])[['COL3','COL4']].apply(numpy.median) 

/Applications/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py in apply(self, func, *args, **kwargs) 
    649   # ignore SettingWithCopy here in case the user mutates 
    650   with option_context('mode.chained_assignment', None): 
--> 651    return self._python_apply_general(f) 
    652 
    653  def _python_apply_general(self, f): 

/Applications/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py in _python_apply_general(self, f) 
    658    keys, 
    659    values, 
--> 660    not_indexed_same=mutated or self.mutated) 
    661 
    662  def _iterate_slices(self): 

/Applications/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py in _wrap_applied_output(self, keys, values, not_indexed_same) 
    3373     coerce = True if any([isinstance(x, Timestamp) 
    3374          for x in values]) else False 
-> 3375     return (Series(values, index=key_index, name=self.name) 
    3376       ._convert(datetime=True, 
    3377         coerce=coerce)) 

    /Applications/anaconda3/lib/python3.5/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath) 
     231   generic.NDFrame.__init__(self, data, fastpath=True) 
     232 
    --> 233   self.name = name 
     234   self._set_axis(0, index, fastpath=True) 
     235 

    /Applications/anaconda3/lib/python3.5/site-packages/pandas/core/generic.py in __setattr__(self, name, value) 

    2692    object.__setattr__(self, name, value) 
    2693   elif name in self._metadata: 
-> 2694    object.__setattr__(self, name, value) 
    2695   else: 
    2696    try: 

/Applications/anaconda3/lib/python3.5/site-packages/pandas/core/series.py in name(self, value) 
    307  def name(self, value): 
    308   if value is not None and not com.is_hashable(value): 
--> 309    raise TypeError('Series.name must be a hashable type') 
    310   object.__setattr__(self, '_name', value) 
    311 

TypeError: Series.name must be a hashable type 
+0

您是否有偶數個值? – CodeCupboard

回答

0

不知何故,現階段的系列名稱被解釋爲不可哈希,儘管可能是一個元組。我想這可能是相同的錯誤作爲一個固定的封閉:

基本上,在羣體(如你在你的例子)單個標值造成的名字該系列不能通過。它固定在0.19.2


在任何情況下,它不應該是一個現實關懷,因爲你可以(也應該)調用meanmedian等上的GroupBy對象直接。

>>> df.groupby(['COL1', 'COL2'])[['COL3', 'COL4']].median() 
      COL3 COL4 
COL1 COL2    
A AA  2.5 0.5 
    BB  4.5 2.5 
B BB  3.0 3.0 
+1

謝謝。您的意見非常有用,並解決了我的問題。 –