Cumulative sum of a column in Pandas – Python

Cumulative sum of a column in Pandas can be easily calculated with the use of a pre-defined function cumsum()
 

Syntax:  cumsum(axis=None, skipna=True, *args, **kwargs)
Parameters: 
axis: {index (0), columns (1)} 
skipna: Exclude NA/null values. If an entire row/column is NA, the result will be NA
Returns: Cumulative sum of the column

Example 1: 
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
import numpy as np
  
# Create a dataframe
df1 = pd.DataFrame({"A":[2, 3, 8, 14], 
                   "B":[1, 2, 4, 3], 
                   "C":[5, 3, 9,2]}) 
  
# Computing sum over Index axis
print(df1.cumsum(axis = 0))

chevron_right


Output: 
 



    A   B   C
0   2   1   5
1   5   3   8
2  13   7  17
3  27  10  19

Example 2: 
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
import numpy as np
  
# Create a dataframe
df1 = pd.DataFrame({"A":[None, 3, 8, 14], 
                   "B":[1, None, 4, 3], 
                   "C":[5, 3, 9,None]}) 
  
# Computing sum over Index axis
print(df1.cumsum(axis = 0, skipna = True))

chevron_right


Output: 
 

      A    B     C
0   NaN  1.0   5.0
1   3.0  NaN   8.0
2  11.0  5.0  17.0
3  25.0  8.0   NaN

Example 3: 
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
import numpy as np
  
# Create a dataframe
df1 = pd.DataFrame({"A":[2, 3, 8, 14], 
                   "B":[1, 2, 4, 3], 
                   "C":[5, 3, 9,2]}) 
  
# Computing sum over Index axis
print(df1.cumsum(axis = 1))

chevron_right


Output: 
 

    A   B   C
0   2   3   8
1   3   5   8
2   8  12  21
3  14  17  19

 




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.