groupby for pandas系列无效

时间:2013-07-29 16:37:47

标签: python pandas

我无法在pandas Series对象上进行groupby。 DataFrames很好,但我似乎无法使用Series进行groupby。有没有人能够让这个工作?

>>> import pandas as pd
>>> a = pd.Series([1,2,3,4], index=[4,3,2,1])
>>> a
4    1
3    2
2    3
1    4
dtype: int64
>>> a.groupby()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/share/apps/install/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 153, in groupby
    sort=sort, group_keys=group_keys)
  File "/share/apps/install/anaconda/lib/python2.7/site-packages/pandas/core/groupby.py", line 537, in groupby
    return klass(obj, by, **kwds)
  File "/share/apps/install/anaconda/lib/python2.7/site-packages/pandas/core/groupby.py", line 195, in __init__
    level=level, sort=sort)
  File "/share/apps/install/anaconda/lib/python2.7/site-packages/pandas/core/groupby.py", line 1326, in _get_grouper
    ping = Grouping(group_axis, gpr, name=name, level=level, sort=sort)
  File "/share/apps/install/anaconda/lib/python2.7/site-packages/pandas/core/groupby.py", line 1203, in __init__
    self.grouper = self.index.map(self.grouper)
  File "/share/apps/install/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 878, in map
    return self._arrmap(self.values, mapper)
  File "generated.pyx", line 2200, in pandas.algos.arrmap_int64 (pandas/algos.c:61221)
TypeError: 'NoneType' object is not callable

3 个答案:

答案 0 :(得分:10)

您需要传递某种映射(可能是dict / function / index)

In [6]: a
Out[6]: 
4    1
3    2
2    3
1    4
dtype: int64

In [7]: a.groupby(a.index).sum()
Out[7]: 
1    4
2    3
3    2
4    1
dtype: int64

In [3]: a.groupby(lambda x: x % 2 == 0).sum()
Out[3]: 
False    6
True     4
dtype: int64

答案 1 :(得分:3)

如果你需要分组系列的值:

grouped = a.groupby(a)

grouped = a.groupby(lambda x: a[x])

答案 2 :(得分:0)

不要把答案当回事;)我并不是说这是个好主意。

如果您真的想要内联或以“流利”的方式进行操作,则可以执行以下操作。

var sendComment = $(".send_comment");
sendComment.submit(function(e) {
    e.preventDefault();     
    var that = $(this);
    $.ajax({
        url : 'save-comment.php',
        type : 'POST',
        dattType : 'html',
        data : $(this).serialize(),
        success : function ( data ) {
            // that.prev(".all_comments").append( data );
            // that.find(".comment_text").val(' ');
            // refresh();   

           sendComment.parent('.comments').first('all_comments').html(data);
        }
    });
});

结果将是

def smart_groupby(self, by=None, *args, **kwargs):
    if by is None:
        return self.groupby(self, *args, **kwargs)
    return self.groupby(by, *args, **kwargs)

import pandas as pd
ps.Series.groupby = smart_groupby

pd.Series(['a', 'a', 'a', 'b', 'b']).groupby().count()

它应该像往常一样运行,但是还有一个好处,就是如果省略a 3 b 2 dtype: int64 ,它将基于自身进行分组。