2

I could not find an explanation for the following behavior.

Here is a very basic table

import pandas as pd
t1 = pd.DataFrame({'date': ['d1','d1','d2','d1'],'id1': ['Apple', 'Pear','Pear','Pear']})  

Here is a simple function I apply to every group id1:

def test(x): 
    print('blblb')
    print(x)
t1.groupby('id1').apply(test)

I would expect the function test to be called only 2 times for the 2 groups but I get the following output:

blblb
  date    id1
0   d1  Apple
blblb
  date    id1
0   d1  Apple
blblb
  date   id1
1   d1  Pear
2   d2  Pear
3   d1  Pear

Could someone explain to me why the group apple caused the function to be called twice? Thanks

Frostic
  • 680
  • 4
  • 11

0 Answers0