How to compute the cumulative sum of a column with Pandas using Python
• 2 minIf you already worked with time-series data, let me tell you a bit more about the cumulative sum function.
The cumulative sum function is extremely helpful when you want to know what is the sum of a variable over time.
Let me give you an example with E-commerce sales data.
# For the dataframe
import pandas as pd
# We create our e-commerce sales_in_usd over time dataframe
df_sales = pd.DataFrame(index=['2021-01-31', '2021-02-28',
'2021-03-31', '2021-04-30',
'2021-05-31', '2021-06-30',
'2021-07-31', '2021-08-31',
'2021-09-30', '2021-10-31',
'2021-11-30', '2021-12-31'],
data={"sales_in_usd" :
[303.0, 591.0, 918.0, 1221.0, 1509.0,
1806.0, 2112.0, 2413.0, 2706.0, 3005.0,
3291.0, 3592.0]})
df_sales["sales_in_usd"].plot(kind='bar',
grid=True,
title="Monthly E-commerce sales")

So far so good!
Now that we have the sales volume per month in USD. One could ask the question how much total sales volume have we done so far.
Using the DataFrame.cumsum() we can compute that metric over time.
In Python
# We compute the cumulative sum
df_sales["total_sales"] = df_sales["sales_in_usd"].cumsum()
Resulting such DataFrame:

And if we plot it,
df_sales["total_sales"].plot(kind='bar', grid=True, title="Total E-commerce sales")

Here you are! You now know how to compute the cumulative sum of a column with Pandas in Python.
More on DataFrames
If you want to know more about DataFrame and Pandas. Check out the other articles I wrote on the topic, just here :