pandas中使用groupby之后进行apply为什么结果会多出一个输出?

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

df为:

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

使用apply之后输出结果如下:


       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 

请问为什么会重复出现,不是最后应该只有两个分组吗

       A  B
    0  4  9
    1  4  2
    2  4  5 
阅读 4.8k
1 个回答
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...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题