Here’s how to calculate cumulative sum in Pandas.
How to calculate cumulative sum in Pandas
To calculate a cumulative sum in Pandas just use a cumsum method which Pandas is offering to you.
import pandas as pd my_df = pd.DataFrame({"my_column1": [9, 2, 3, 5], "my_column2": [3, 7, 6, 4], "my_column3": [4, 8, 8, 8]}) print(f'The cumulative sum of columns:\n{my_df.cumsum()}')
The cumulative sum of columns: my_column1 my_column2 my_column3 0 9 3 4 1 11 10 12 2 14 16 20 3 19 20 28
For more details see the documentation of cumsum function.
2 thoughts on “How to calculate cumulative sum in Pandas”