“中的简单groupby示例失败”;用于数据分析的Python“;文本

“中的简单groupby示例失败”;用于数据分析的Python“;文本,python,pandas,Python,Pandas,我刚开始学习python(主要是使用“ipython--pylab”作为matlab的开源替代品),阅读了“python for Data Analysis”文本中的示例。在第253页,使用“groupby”(传递数组列表)显示了一个简单的示例。我完全按照文本中的内容重复了一遍,但出现了以下错误: TypeError:“Series”对象是可变的,因此无法对其进行哈希 -----打字错误的详细信息------- TypeError回溯(最近一次调用) 在() ---->1表示=df['data1

我刚开始学习python(主要是使用“ipython--pylab”作为matlab的开源替代品),阅读了“python for Data Analysis”文本中的示例。在第253页,使用“groupby”(传递数组列表)显示了一个简单的示例。我完全按照文本中的内容重复了一遍,但出现了以下错误: TypeError:“Series”对象是可变的,因此无法对其进行哈希

-----打字错误的详细信息-------

TypeError回溯(最近一次调用)
在()
---->1表示=df['data1'].groupby(df['key1'],df['key2']).mean()
/groupby中的home/joeblow/enthught/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/generic.pyc(self、by、axis、level、as_index、sort、group_keys、squence)
2725
2726从pandas.core.groupby导入groupby
->2727轴=自身。获取轴编号(轴)
2728返回groupby(self,by,axis=axis,level=level,as_index=as_index,
2729排序=排序,组\键=组\键,挤压=挤压)
/home/joeblow/enthught/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/generic.pyc in_get_axis_number(self,axis)
283
284 def_获取_轴编号(自身,轴):
-->285 axis=self.\u axis\u别名.get(axis,axis)
286如果com.是_整数(轴):
287如果轴在自身中。\u轴\u名称:
/home/joeblow/enthunk/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/generic.pyc in_uuuuu散列(self)
639定义哈希(自):
640 raise TypeError(“{0!r}对象是可变的,因此不能
-->641“散列”。格式(self.\uuuuuuu类\uuuuuuuu名称)
642
643定义(自身):
TypeError:“Series”对象是可变的,因此无法对其进行散列


我在这里遗漏了什么简单的东西?

您没有完全按照文本中的内容进行操作。:^)

若要按两个数组分组,则需要传递数组列表。相反,您传递了两个参数:
(df['key1'],df['key2'])
,它们被和
轴解释为

import pandas as pd
from pandas import DataFrame

df = DataFrame({'key1' : ['a','a','b','b','a'],'key2' : ['one','two','one','two\
','one'],'data1' : np.random.randn(5),'data2' : np.random.randn(5)})

grouped = df['data1'].groupby(df['key1'])
means = df['data1'].groupby(df['key1'],df['key2']).mean()
TypeError                                 Traceback (most recent call last)
<ipython-input-7-0412f2897849> in <module>()
----> 1 means = df['data1'].groupby(df['key1'],df['key2']).mean()

/home/joeblow/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/generic.pyc in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze)
   2725 
   2726         from pandas.core.groupby import groupby
-> 2727         axis = self._get_axis_number(axis)
   2728         return groupby(self, by, axis=axis, level=level, as_index=as_index,
   2729                        sort=sort, group_keys=group_keys, squeeze=squeeze)

/home/joeblow/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_axis_number(self, axis)
    283 
    284     def _get_axis_number(self, axis):
--> 285         axis = self._AXIS_ALIASES.get(axis, axis)
    286         if com.is_integer(axis):
    287             if axis in self._AXIS_NAMES:

/home/joeblow/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/generic.pyc in __hash__(self)
    639     def __hash__(self):
    640         raise TypeError('{0!r} objects are mutable, thus they cannot be'
--> 641                         ' hashed'.format(self.__class__.__name__))
    642 
    643     def __iter__(self):

TypeError: 'Series' objects are mutable, thus they cannot be hashed
>>> means = df['data1'].groupby([df['key1'],df['key2']]).mean()
>>> means
key1  key2
a     one     1.127536
      two     1.220386
b     one     0.402765
      two    -0.058255
dtype: float64