Here is data
| id | date | population |
|---|---|---|
| 1 | 2021-5 | 21 |
| 2 | 2021-5 | 22 |
| 3 | 2021-5 | 23 |
| 4 | 2021-5 | 24 |
| 1 | 2021-4 | 17 |
| 2 | 2021-4 | 24 |
| 3 | 2021-4 | 18 |
| 4 | 2021-4 | 29 |
| 1 | 2021-3 | 20 |
| 2 | 2021-3 | 29 |
| 3 | 2021-3 | 17 |
| 4 | 2021-3 | 22 |
I want to calculate the monthly change regarding population in each id. so result will be:
| id | date | delta |
|---|---|---|
| 1 | 5 | .2353 |
| 1 | 4 | -.15 |
| 2 | 5 | -.1519 |
| 2 | 4 | -.2083 |
| 3 | 5 | .2174 |
| 3 | 4 | .0556 |
| 4 | 5 | -.2083 |
| 4 | 4 | .3182 |
delta := (this month - last month) / last month
How to approach this in pandas? I'm thinking of groupby but don't know what to do next
remember there might be more dates. but results is always