gpt4 book ai didi

pandas cumsum 跳过专栏

转载 作者:行者123 更新时间:2023-12-05 00:40:18 25 4
gpt4 key购买 nike

我是 pandas 的新手,我可以将 cumsum 添加为

df.cumsum(axis=1)

   y0  y1  y2
0 2 3 4
1 2 2 3
2 0 0 0
3 1 2 3
y0 y1 y2
0 2 5 9
1 2 4 7
2 0 0 0
3 1 3 6

但是有没有办法只在前 2 列上执行,即跳过 y2?

最佳答案

你需要排除y2,找到cumsum和concat y2回来。

pd.concat([df[['y0', 'y1']].cumsum(axis=1),df['y2']], axis=1)

输出:

    y0  y1  y2
0 2 5 4
1 2 4 3
2 0 0 0
3 1 3 3

关于pandas cumsum 跳过专栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50688670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com