Vivek Mehendiratta
Vivek Mehendiratta

Reputation: 213

How .apply works in Pandas?

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.

Upvotes: 2

Views: 220

Answers (1)

Roald
Roald

Reputation: 2979

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.

Upvotes: 2

Related Questions