来自" Python for Data Analysis"的简单groupby示例失败了文本

时间:2014-06-19 20:30:36

标签: python pandas

我刚开始学习python(主要是使用“ipython --pylab”作为matlab的开源替代品),通过“Python for Data Analysis”文本中的示例。在页253,使用'groupby'(传递数组列表)显示一个简单的示例。我完全按照文本重复它,但我收到此错误: “TypeError:'Series'对象是可变的,因此它们不能被散列”

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的细节-------

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

我在这里错过了什么简单的事情?

1 个答案:

答案 0 :(得分:3)

你没有完全像在文本中那样做。 :^)

>>> 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

如果您要按两个数组进行分组,则需要传递一个数组列表。您改为传递两个参数:(df['key1'],df['key2']),它们被解释为byaxis