I am trying to calculate the percent change by month for each product. Here is what I have so far. I have this working for a DataFrame involving a single product. I am stumped on how to get the calculation applied to a result set that contains many products and many months.
Example dataframe:
product_desc activity_month prod_count
product_a 1/1/2014 53
product_b 1/1/2014 42
product_c 1/1/2014 38
product_a 2/1/2014 26
product_b 2/1/2014 48
product_c 2/1/2014 39
product_a 3/1/2014 41
product_b 3/1/2014 35
product_c 3/1/2014 50
What I need to get out is the dataframe with a percentage change by product_desc by month added to it:
product_desc activity_month prod_count pct_change
product_a 1/1/2014 53
product_a 2/1/2014 26 0.490566038
product_a 3/1/2014 41 1.576923077
product_b 1/1/2014 42
product_b 2/1/2014 48 1.142857143
product_b 3/1/2014 35 0.729166667
product_c 1/1/2014 38
product_c 2/1/2014 39 1.026315789
product_c 3/1/2014 50 1.282051282
I can calculate this on a dataframe with a single product_desc with this:
df['change_rate1'] = df['prod_count'].shift(-1)/df['prod_count']
df['pct_change'] = df['change_rate1'].shift(1)
df = df.drop('change_rate1',1)
Here is what I am trying now:
df_grouped = df.groupby(['product_desc','activity_month'])
for product_desc, activity_month in df_grouped:
df['change_rate1'] = df_grouped['prod_count'].shift(-1)/df_grouped['prod_count']
However, I get back a 'NotImplementedError' on the last line in the for statement.
Any advice on how to get this calculated correctly is appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…