本文介绍了DataFrame groupby().apply()函数似乎被调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个简单的DataFrame:
Here is a simple DataFrame:
import numpy as np
dd=pd.DataFrame(np.arange(35).reshape(7,5), columns=list('xyzwv'))
dd['w']=list('AABBBCC')
,即:
现在我尝试以下代码
def func(x):
print(x)
return x
dd.groupby('w').apply(func)
然后将其打印出来:
我认为出了点问题,因为被打印两次.
I think something goes wrong because is being printed twice.
对于同一组,好像func()被调用了两次.我犯了什么错误?
It looks as if func() is being called twice for the same group.What mistake did I do?
推荐答案
apply
两次调用第一组以确定是否可以进行一些优化,请参见 http://pandas.pydata.org/pandas-docs/stable/groupby.html#flexible-apply
apply
calls the first group twice to work out whether it could do some optimizations, seehttp://pandas.pydata.org/pandas-docs/stable/groupby.html#flexible-apply
这篇关于DataFrame groupby().apply()函数似乎被调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!