열에 대한 그룹의 중앙값을 계산하려고합니다. 나는팬더 열의 그룹 중앙값 계산
Pandas: Calculate Median of Group over Columns
이 질문에 매우 분명한 예를 발견하고 대답은 내가 필요 정확히 답변입니다. 나는에 대한 세부 사항을 통해 작업을 게시 정확한 예제를 만들어 내 자신의 내가 오류
TypeError: Series.name must be a hashable type
에게 발생하는 열을 통해 그룹의 평균을 계산하려고
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 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
짝수 개의 값이 있습니까? – CodeCupboard