Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cumsum as a new column in an existing Pandas data

I have a pandas dataframe defined as:

A   B   SUM_C       1   1   10      1   2   20    

I would like to do a cumulative sum of SUM_C and add it as a new column to the same dataframe. In other words, my end goal is to have a dataframe that looks like below:

A   B   SUM_C   CUMSUM_C        1   1   10      10      1   2   20      30    

Using cumsum in pandas on group() shows the possibility of generating a new dataframe where column name SUM_C is replaced with cumulative sum. However, my ask is to add the cumulative sum as a new column to the existing dataframe.

Thank you

like image 868
user1124702 Avatar asked Jan 25 '17 18:01

user1124702


People also ask

How do you make new columns derived from existing columns in Python?

¶ To create a new column, use the [] brackets with the new column name at the left side of the assignment.

How do you use cumulative sum in pandas?

Pandas DataFrame cumsum() Method 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.


1 Answers

Just apply cumsum on the pandas.Series df['SUM_C'] and assign it to a new column:

df['CUMSUM_C'] = df['SUM_C'].cumsum() 

Result:

df Out[34]:     A  B  SUM_C  CUMSUM_C 0  1  1     10       10 1  1  2     20       30 
like image 94
blacksite Avatar answered Sep 19 '22 15:09

blacksite