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

发布于 2022-09-07 11:37:40 字数 509 浏览 11 评论 0

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2022-09-14 11:37:40

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文