DataFrame groupby().apply()函数似乎被调用了两次 [英] DataFrame groupby().apply() function seems to be called twice

查看:329
本文介绍了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, see http://pandas.pydata.org/pandas-docs/stable/groupby.html#flexible-apply

这篇关于DataFrame groupby().apply()函数似乎被调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆