My data is like this:
| year_month | user_id | pageviews | visits |
|---|---|---|---|
| 2020-03 | 2 | 8 | 3 |
| 2021-03 | 27 | 4 | 3 |
| 2021-05 | 23 | 75 | 7 |
| 2020-05 | 23 | 17 | 7 |
| 2020-08 | 339 | 253 | 169 |
| 2020-08 | 892 | 31 | 4 |
| 2021-08 | 339 | 4 | 3 |
And I wanted to group by year_month calculating the difference of pageviews and visits from one year(2020) to the next(2021).
So, I was thinking the output should be something similar to (without the content inside the parenthesis):
| last_month | diff(pageviews) | diff(visits) |
|---|---|---|
| 2021-03 | -4(4-8) | 0(3-3) |
| 2021-05 | 58(75-17) | 0(7-7) |
| 2021-08 | -280(4-284) | -170(3-173) |
But I'm not sure how to do it vectorized, I was thinking of passing it to pandas and doing it with a for loop, but wanted to learn how to do this kind of things in a vectorized way with pyspark or sparksql that I think they will be much faster.