Skip to content
Advertisement

How to calculate running total per customer for previous 365 days in pandas

I am trying to calculate a running total per customer for the previous 365 days using pandas but my code isn’t working. My intended output would be something like this:

date customer daily_total_per_customer rolling_total
2016-07-29 1 100 100
2016-08-01 1 50 150
2017-01-12 1 80 230
2017-10-23 1 180 260
2018-03-03 1 0 180
2018-03-06 1 40 220
2019-03-16 1 500 500
2017-04-07 2 50 50
2017-04-09 2 230 280
2018-02-11 2 80 360
2018-05-12 2 0 80
2019-05-10 2 0 0

I tried the following:

JavaScript

And I get the following error

ValueError: invalid on specified as date, must be a column (of DataFrame), an Index or None To recreate the code:

JavaScript

Perhaps someone can point me in the right direction. Thanks!

Advertisement

Answer

Annotated code

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement