Cumsum as a new column in an existing Pandas data
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
1 Answer
1
Just apply cumsum on the pandas.Series df['SUM_C'] and assign it to a new column:
cumsum
pandas.Series
df['SUM_C']
df['CUMSUM_C'] = df['SUM_C'].cumsum(axis = 0)
Result:
df
Out[34]:
A B SUM_C CUMSUM_C
0 1 1 10 10
1 1 2 20 30
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you. Correct answer for the question that was posed. I would like to also add that if we have more than two columns and would like to do the same, they process is below: Step:1: obviously work with a sorted data frame 2. Use df['CUM_D'] = df.groupby(['A','B'])['D'].cumsum(axis = 0). skipped 'C' in groupby as it is the column on which cumulative summation is being done.
– user1124702
Jan 25 '17 at 19:53