python cumsum用法

介绍:

cumsum函数是pandas的累加函数,用来求列的累加值。

函数用法:

DataFrame.cumsum(axis=None, skipna=True, args, kwargs)

举例说明:

s1 = pd.Series([0,1,2,3,4])
print(s1)
0    0
1    1
2    2
3    3
4    4
print(s1.cumsum())
dtype: int64
0     0
1     1
2     3
3     6
4    10
dtype: int64
data = np.random.randint(0,25,25).reshape(5,5)
columns = list("ABCDE")
p1 = pd.DataFrame(data,columns = columns)
print(p1)
    A   B   C   D   E
0   7   1   5   2   6
1   4   9  16   6  23
2   6   0  13   0  19
3  18  14  18  22  10
4  11   0   4  15  14
p1["F"] = p1["A"].cumsum()
print(p1)
    A   B   C   D   E   F
0   7   1   5   2   6   7
1   4   9  16   6  23  11
2   6   0  13   0  19  17
3  18  14  18  22  10  35
4  11   0   4  15  14  46
print(p1.cumsum(axis = 1))
    A   B   C   D   E    F
0   7   8  13  15  21   28
1   4  13  29  35  58   69
2   6   6  19  19  38   55
3  18  32  50  72  82  117
4  11  11  15  30  44   90
print(p1.iloc[1].cumsum())
A     4
B    13
C    29
D    35
E    58
F    69
Name: 1, dtype: int32

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值