Reputation: 580
I have a df as
I want to filter get percent change and total change between the different quarters for YoY.
I am not sure how can i start this in python, any help will be appreciated.
Considering i have many parameters for Name. i need to use the group by filter for each KPI, YEAR and Quarter, i tried
d2['d'] = d2.groupby(['KPI', 'Quarter', ])['Number'].apply( lambda x: (x - x.shift(4))/x )
Upvotes: 0
Views: 102
Reputation: 862406
Use Series,pct_change
per groups, multiple by mul
and if necessary round
:
d2['Percent Change'] = (d2.groupby(['KPI', 'Quarter'])['Number']
.transform(pd.Series.pct_change)
.mul(100)
.round())
For total change add cumsum
d2['Total Change'] = d2['Number'].pct_change().cumsum().mul(100).round().fillna(0)
Upvotes: 1