DataFrame groupby().apply() function seems to be called twice

Viewed 924

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')

,which is:

enter image description here

Now I try the following code

def func(x):
    print(x)
    return x

dd.groupby('w').apply(func)

then it prints out:

enter image description here

I think something goes wrong because enter image description here is being printed twice.

It looks as if func() is being called twice for the same group. What mistake did I do?

1 Answers
Related