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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bec9c8-31057.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bec9c8-31057.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?