s_mj
s_mj

Reputation: 580

comparison between the values in pandas dataframe

I have a df as

enter image description here

I want to filter get percent change and total change between the different quarters for YoY.

enter image description here

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

Answers (1)

jezrael
jezrael

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

Related Questions