My DataFrame looks like this:
| id | date | value |
|---|---|---|
| 1 | 2021-07-16 | 100 |
| 2 | 2021-09-15 | 20 |
| 1 | 2021-04-10 | 50 |
| 1 | 2021-08-27 | 30 |
| 2 | 2021-07-22 | 15 |
| 2 | 2021-07-22 | 25 |
| 1 | 2021-06-30 | 40 |
| 3 | 2021-10-11 | 150 |
| 2 | 2021-08-03 | 15 |
| 1 | 2021-07-02 | 90 |
I want to groupby the id, and return the difference of total value in a 90-days period. Specifically, I want the values of last 90 days based on today, and based on 30 days ago.
For example, considering today is 2021-10-13, I would like to get:
- the sum of all values per id between 2021-10-13 and 2021-07-15
- the sum of all values per id between 2021-09-13 and 2021-06-15
And finally, subtract them to get the variation.
I've already managed to calculate it, by creating separated temporary dataframes containing only the dates in those periods of 90 days, grouping by id, and then merging these temp dataframes into a final one. But I guess it should be an easier or simpler way to do it. Appreciate any help!
Btw, sorry if the explanation was a little messy.