python对象group函数_如何对groupby对象应用多个函数
这是一个很好的机会来强调熊猫0.20的变化之一
这是什么意思?
考虑数据帧dfdf = pd.DataFrame(dict(
A=np.tile([1, 2], 2).repeat(2),
B=np.repeat([1, 2], 2).repeat(2),
C=np.arange(8)
))
df
A B C
0 1 1 0
1 1 1 1
2 2 1 2
3 2 1 3
4 1 2 4
5 1 2 5
6 2 2 6
7 2 2 7
我们以前可以做的
^{pr2}$
我们的名字'f1'和{}被放在列标题中。但是,熊猫0.20我得到了这个//anaconda/envs/3.6/lib/python3.6/site-packages/ipykernel/__main__.py:1: FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
if __name__ == '__main__':
那这是什么意思?如果我在没有命名字典的情况下做两个lambdas怎么办?在df.groupby(['A', 'B']).C.agg([lambda x: x.size, lambda x: x.max()])
-
SpecificationError Traceback (most recent call last)
in ()
> 1 print(df.groupby(['A', 'B']).C.agg([lambda x: x.size, lambda x: x.max()]))
//anaconda/envs/3.6/lib/python3.6/site-packages/pandas/core/groupby.py in aggregate(self, func_or_funcs, *args, **kwargs)
2798 if hasattr(func_or_funcs, '__iter__'):
2799 ret = self._aggregate_multiple_funcs(func_or_funcs,
-> 2800 (_level or 0) + 1)
2801 else:
2802 cyfunc = self._is_cython_func(func_or_funcs)
//anaconda/envs/3.6/lib/python3.6/site-packages/pandas/core/groupby.py in _aggregate_multiple_funcs(self, arg, _level)
2863 if name in results:
2864 raise SpecificationError('Function names must be unique, '
-> 2865 'found multiple named %s' % name)
2866
2867 # reset the cache so that we
SpecificationError: Function names must be unique, found multiple named
pandas在名为''的多个列上出现错误
解决方案:命名函数def f1(x):
return x.size
def f2(x):
return x.max()
df.groupby(['A', 'B']).C.agg([f1, f2])
f1 f2
A B
1 1 2 1
2 2 5
2 1 2 3
2 2 7
总结
以上是生活随笔为你收集整理的python对象group函数_如何对groupby对象应用多个函数的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python 节气计算_python 生
- 下一篇: python调用kafka拉取数据失败_