I have the following dataframe
:
In [1]: df
Out[1]:
ID Month Transaction_Amount
1 2013/01 10
1 2013/02 20
1 2013/03 10
1 2013/04 20
1 2013/05 10
1 2013/06 20
1 2013/07 10
1 2013/08 20
1 2013/09 10
1 2013/10 20
1 2013/11 10
1 2013/12 20
1 2014/01 15
1 2014/02 25
1 2014/03 15
1 2014/04 25
...
1 2014/11 15
1 2014/12 25
...
10000000 2014/11 13
10000000 2014/12 23
What I would like to do is calculate the growth over rolling month periods year over year, so for example, I would want to find the value of (2014/01 - 2013/01) / (2014/01)
which is (15 - 10) / (15) = 1/3
and save this for the first rolling period. There will be a total of 12 rolling periods for each ID
. I'm thinking that the final output should look like:
In [2]: df_new
Out[2]:
ID rolling_period_1 rolling_period_2 ... rolling_period_12
1 .333333 .25 .25
2 x1 x2 x12
3 y1 y2 y12
4 z1 z2 z12
...
I generated a list containing tuples of every year over period [(2013/01, 2014/01), (2013/02, 2014/02) ... (2013/12, 2014/12)]
and have been playing around with isin
to index a subset of the original df
, but I am unsure how to arrive at the df_new
.
EDIT
I have created a new dataframe called temp_df
with the following code:
In [4]: temp_df = df[df['month'].isin(('2013/01','2014/01'))]
In [5]: temp_df
Out[5]:
ID Month Transaction_Amount
1 2013/01 10
1 2014/01 15
2 2013/01 20
2 2014/01 30
3 2013/01 15
3 2014/01 30
...
What I would like to produce is a DataFrame
that looks like the following:
In [6]: new_df
Out[6]:
ID Transaction_Growth
1 .3333 # (15-10)/15
2 .3333 # (30-20)/30
3 .50 # (30-15)/30
...