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

查看:37
本文介绍了DataFrame groupby().apply() 函数似乎被调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的数据帧:

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天全站免登陆