介绍:
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