How can I calculate a reversed cumulative sum in Pandas?

How can I calculate a reversed cumulative sum in Pandas?

Pandas is a popular library in Python used for data manipulation and analysis. One common task in data analysis is to calculate the cumulative sum of a series of numbers. However, there may be instances where it is necessary to calculate the reversed cumulative sum, where the sum is calculated in the reverse order. This can be achieved in Pandas by using the “cumsum” function and then reversing the resulting series using the “iloc” function. This process involves first calculating the cumulative sum, then subtracting each value from the total sum, and finally reversing the order of the resulting series. This method allows for the efficient calculation of the reversed cumulative sum in Pandas.

Calculate a Reversed Cumulative Sum in Pandas


The 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:

Cite this article

stats writer (2024). How can I calculate a reversed cumulative sum in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-a-reversed-cumulative-sum-in-pandas/

stats writer. "How can I calculate a reversed cumulative sum in Pandas?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-a-reversed-cumulative-sum-in-pandas/.

stats writer. "How can I calculate a reversed cumulative sum in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-a-reversed-cumulative-sum-in-pandas/.

stats writer (2024) 'How can I calculate a reversed cumulative sum in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-a-reversed-cumulative-sum-in-pandas/.

[1] stats writer, "How can I calculate a reversed cumulative sum in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate a reversed cumulative sum in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top