Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running sum in pandas (without loop)

Tags:

python

pandas

I'd like to build a running sum over a pandas dataframe. I have something like:

10/10/2012:  50,  0
10/11/2012: -10, 90
10/12/2012: 100, -5

And I would like to get:

10/10/2012:  50,  0
10/11/2012:  40, 90
10/12/2012: 140, 85

So every cell should be the sum of itself and all previous cells, how should I do this without using a loop.

like image 628
leo Avatar asked Dec 14 '12 12:12

leo


People also ask

How do you do cumulative sum in pandas?

The cumsum() method returns a DataFrame with the cumulative sum for each row. The cumsum() method goes through the values in the DataFrame, from the top, row by row, adding the values with the value from the previous row, ending up with a DataFrame where the last row contains the sum of all values for each column.

How do you find the sum of a column in pandas?

sum() function return the sum of the values for the requested axis. If the input is index axis then it adds all the values in a column and repeats the same for all the columns and returns a series containing the sum of all the values in each column.


1 Answers

As @JonClements mentions, you can do this using the cumsum DataFrame method:

from pandas import DataFrame
df = DataFrame({0: {'10/10/2012': 50, '10/11/2012': -10, '10/12/2012': 100}, 1: {'10/10/2012': 0, '10/11/2012': 90, '10/12/2012': -5}})

In [3]: df
Out[3]: 
              0   1
10/10/2012   50   0
10/11/2012  -10  90
10/12/2012  100  -5

In [4]: df.cumsum()
Out[4]: 
              0   1
10/10/2012   50   0
10/11/2012   40  90
10/12/2012  140  85
like image 86
Andy Hayden Avatar answered Oct 21 '22 06:10

Andy Hayden