Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate month by month change in value per user in pandas?

I was looking for similar topics, but I found only change by month. My problem is that I would like to have a month change in value e.g. UPL but per user like in the below example.

user_id month UPL
1 2022-01-01 00:00:00 100
1 2022-02-01 00:00:00 200
2 2022-01-01 00:00:00 100
2 2022-02-01 00:00:00 50
1 2022-03-01 00:00:00 150

And to have additional column named "UPL change month by month":

user_id month UPL UPL_change_by_month
1 2022-01-01 00:00:00 100 0
1 2022-02-01 00:00:00 200 100
2 2022-01-01 00:00:00 100 0
2 2022-02-01 00:00:00 50 -50
1 2022-03-01 00:00:00 150 -50

Is it possible using aggfunc or shift function using Pandas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

IIUC, you can use groupby_diff:

df['UPL_change_by_month'] = df.sort_values('month').groupby('user_id')['UPL'].diff().fillna(0)
print(df)

# Output
   user_id      month  UPL  UPL_change_by_month
0        1 2022-01-01  100                  0.0
1        1 2022-02-01  200                100.0
2        2 2022-01-01  100                  0.0
3        2 2022-02-01   50                -50.0
4        1 2022-03-01  150                -50.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading