Reputation: 1387
I have data that looks like this:
YearMonth PageViews Users
202001 100 10
202002 150 12
202003 100 13
202004 120 15
202005 130 10
I want to find out the percentage difference of each new month from the average of the previous month's usage. For example,
How can I find this using python? Any help would be appreciated. Thank you.
Upvotes: 0
Views: 257
Reputation: 8768
Alternative using df.expanding().mean()
((df[['PageViews','Users']]
.div(df[['PageViews','Users']].expanding().mean().shift()))
.sub(1))
Output:
PageViews Users
0 NaN NaN
1 0.500000 0.200000
2 -0.200000 0.181818
3 0.028571 0.285714
4 0.106383 -0.200000
Upvotes: 0
Reputation: 150745
Try cumsum()/range
for the cumulative mean:
cols = ['PageViews','Users']
cum_mean = df[cols].cumsum().div(np.arange(len(df))+1, axis=0)
df[cols].div(cum_mean.shift()).sub(1)
Output:
PageViews Users
0 NaN NaN
1 0.500000 0.200000
2 -0.200000 0.181818
3 0.028571 0.285714
4 0.106383 -0.200000
Upvotes: 2