Reputation: 99
I have a dataframe like this:
t; value
5; 10
4; 0
3; 2
2; 0
1; 1
What I want is to sum iterative over the columns in row "values" so that I get this:
t; value; sum
5; 10; 13
4; 0; 3
3; 2; 3
2; 0; 1
1; 1; 1
How I can do this in python with dataframe?
t = time
Upvotes: 1
Views: 57
Reputation: 57033
There is a function cumsum
that calculates the cumulative sum from the top to the bottom. Reverse the dataframe, then calculate the sum:
df['sum'] = df.loc[::-1, 'value'].cumsum()
# t value sum
#0 5 10 13
#1 4 0 3
#2 3 2 3
#3 2 0 1
#4 1 1 1
Upvotes: 2