Skip to content Skip to sidebar Skip to footer

Calculating Year Over Year Growth By Group In Pandas

I have the following dataframe: In [1]: df Out[1]: ID Month Transaction_Amount 1 2013/01 10 1 2013/02 20 1

Solution 1:

I think there is a much more simple way to do this that doesn't require keeping track of shifting time periods, try using the df.pct_change() method:

importpandasaspdimportnumpyasnpdate_range=pd.period_range("2016-01","2018-01",freq='m')df=pd.DataFrame({'A':np.random.rand(len(date_range))},index=date_range)df['pct_pop']=df['A'].pct_change()df['pct_yoy']=df['A'].pct_change(12)dfApct_poppct_yoy2016-01 0.478381NaNNaN2016-02 0.9414500.967991NaN2016-03 0.128445-0.863567NaN2016-04 0.4986232.882011NaN2016-05 0.9146630.834377NaN2016-06 0.349565-0.617821NaN2016-07 0.5632960.611419NaN2016-08 0.144055-0.744264NaN2016-09 0.5022792.486708NaN2016-10 0.6212830.236928NaN2016-11 0.7168130.153763NaN2016-12 0.152372-0.787431NaN2017-01 0.1606360.054234-0.6642092017-02 0.4967592.092453-0.4723472017-03 0.324318-0.3471321.5249652017-04 0.4316510.330949-0.1343152017-05 0.9730951.2543570.0638842017-06 0.007917-0.991864-0.9773512017-07 0.875365109.5628700.5540052017-08 0.860987-0.0164254.9767842017-09 0.099549-0.884378-0.8018052017-10 0.5442754.467398-0.1239502017-11 0.433326-0.203846-0.3954822017-12 0.6880570.5878503.5156362018-01 0.9240380.3429674.752374

Solution 2:

you can use shift to offset the rows in the dataframe.

Create dummy data with month column and values column

rng = pd.date_range('1/1/2011', periods=90, freq='M')
df = pd.DataFrame({'value':range(1,91),'date':rng})

set the month column to index

df = df.set_index('date')

shift a copy of the dataframe 12 periods to get the value 12 months ago, minus from the current record, and divide by current record:

df - df.shift(12)/ df

update the sign on the period in the shift function

Updated to consider ID

# Create range of months
rng = pd.date_range('1/1/2011', periods=180, freq='M')
ID = np.array([1,2,3])

# Create ID column
ID = np.repeat(ID,60)

# Create dummy data in dataframe
df = pd.DataFrame({'ID':ID,'value':range(1,181),'date':rng})

# Use shift on a group by object
(df.value - df.groupby(['ID']).value.shift(12))/ df.value

Post a Comment for "Calculating Year Over Year Growth By Group In Pandas"