I have a dataframe like this (many rows):
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
a 34 24 47 30 11 57 47 44 22 33 16 39
b 50 53 42 23 19 29 38 46 21 18 13 24
. . .
.
. . .
I would like to create a new df with the rolling 3 month percentage change values, so the [1,1] element will be the % change between the value of Apr and the value of Jan, the [1,2] element wiil be the % change between May and Feb etc... Therefore, for each value, I want the % change between this value and the value 3 months ago
This is the sample output that I want (for example the first value is
[(30-34)/34]*100 = -11.7):
Apr May Jun Jul Aug Sep Oct Nov Dec
a -11.7% -54.1% 0% 56.6% 300% .. .. .. ..
. .
. .
I know that pandas have the .pct_change
but this does not calculate the percentages in the way that I want. Any ideas on how I can do this in python?
Thank you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…