2

I was going through this question where Ted Petrou explains the difference between .transform and .apply

This is the DataFrame used

df = pd.DataFrame({'State':['Texas', 'Texas', 'Florida', 'Florida'], 
                   'a':[4,5,1,3], 'b':[6,10,3,11]})

      State  a   b
 0    Texas  4   6
 1    Texas  5  10
 2  Florida  1   3
 3  Florida  3  11

Function inspect is defined

def inspect(x):
    print(x)

When I call inspect function using apply, I get 3 dataframes instead of 2

df.groupby('State').apply(lambda x:inspect(x))
     State  a   b
2  Florida  1   3
3  Florida  3  11

     State  a   b
2  Florida  1   3
3  Florida  3  11

   State  a   b
0  Texas  4   6
1  Texas  5  10

Why am I getting 3 dataframes, instead of 2 while printing ? I really want to know how apply function works?

Thanks in advance.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
Vivek Mehendiratta
  • 203
  • 1
  • 4
  • 11

1 Answers1

2

From the docs:

In the current implementation apply calls func twice on the first column/row to decide whether it can take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will take effect twice for the first column/row.

Roald
  • 2,459
  • 16
  • 43