Why is there an extra output for apply after using groupby in pandas?

df = pd.DataFrame([[4, 9],[4, 2], [4, 5], [5, 4]], columns=["A", "B"])
df.groupby(["A"]).apply(lambda x : print(x, "\n"))

df is:

  A  B
0  4  9
1  4  2
2  4  5
3  5  4

the output after using apply is as follows:


       A  B
    0  4  9
    1  4  2
    2  4  5 
    
       A  B
    0  4  9
    1  4  2
    2  4  5 
    
       A  B
    3  5  4 

Why is it repeated? shouldn"t there be only two groups in the end

       A  B
    0  4  9
    1  4  2
    2  4  5 
Mar.16,2021

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.

refer: http://pandas.pydata.org/pand.

Menu