groupby for pandas系列不起作用 [英] groupby for pandas Series not working
本文介绍了groupby for pandas系列不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法对熊猫系列对象进行分组. DataFrames很好,但是我似乎无法通过Series进行groupby.有人能使它正常工作吗?
I am unable to do a groupby on a pandas Series object. DataFrames are fine, but I cannot seem to do groupby with a Series. Has anyone been able to get this to work?
>>> 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
推荐答案
您需要传递某种映射(可能是dict/function/index)
You need to pass a mapping of some kind (could be a 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
这篇关于groupby for pandas系列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文