23.6 C
London
Friday, July 25, 2025
HomePandas in PythonDataFrame Functions in PythonHow to Calculate a Reversed Cumulative Sum in Pandas

How to Calculate a Reversed Cumulative Sum in Pandas

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The cumsum() function can be used to calculate the cumulative sum of values in a column of a pandas DataFrame.

You can use the following syntax to calculate a reversed cumulative sum of values in a column:

df['cumsum_reverse'] = df.loc[::-1, 'my_column'].cumsum()[::-1]

This particular syntax adds a new column called cumsum_reverse to a pandas DataFrame that shows the reversed cumulative sum of values in the column titled my_column.

The following example shows how to use this syntax in practice.

Example: Calculate a Reversed Cumulative Sum in Pandas

Suppose we have the following pandas DataFrame that shows the total sales made by some store during 10 consecutive days:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'day': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                   'sales': [3, 6, 0, 2, 4, 1, 0, 1, 4, 7]})

#view DataFrame
df

      day   sales
0	1	3
1	2	6
2	3	0
3	4	2
4	5	4
5	6	1
6	7	0
7	8	1
8	9	4
9	10	7

We can use the following syntax to calculate a reversed cumulative sum of the sales column:

#add new column that shows reverse cumulative sum of sales
df['cumsum_reverse_sales'] = df.loc[::-1, 'sales'].cumsum()[::-1]

#view updated DataFrame
df

	day	sales	cumsum_reverse_sales
0	1	3	28
1	2	6	25
2	3	0	19
3	4	2	19
4	5	4	17
5	6	1	13
6	7	0	12
7	8	1	12
8	9	4	11
9	10	7	7

The new column titled cumsum_reverse_sales shows the cumulative sales starting from the last row.

Here’s how we would interpret the values in the cumsum_reverse_sales column:

  • The cumulative sum of sales for day 10 is 7.
  • The cumulative sum of sales for day 10 and day 9 is 11.
  • The cumulative sum of sales for day 10, day 9, and day 8 is 12.
  • The cumulative sum of sales for day 10, day 9, day 8, and day 7 is 12.

And so on.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Sum Specific Columns in Pandas
How to Perform a GroupBy Sum in Pandas
How to Sum Columns Based on a Condition in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories