Home > Software design >  Is it possible to use DataFrame.pct_change and retain all columns?
Is it possible to use DataFrame.pct_change and retain all columns?

Time:09-08

Is there a way to use the pandas percent change function (DataFrame.pct_change) inplace or in some fashion that retains all the columns of the initial dataframe?

For instance,

PctChange = CH1['Area_SqM'].pct_change()    

creates a pandas series of percent change values. But I would prefer to keep all the columns and have percent change be added as an additional column--the code snippet below would work w/ a different function, but inplace is not an available parameter here.

CH1['Area_SqM'].pct_change(inplace=True)

Is there a way to calculate the percent change and retain all columns in one line of code? I've been doing a join after the fact, but i'm iterating this over a lot of tables and would like to keep it streamlined.

Many thanks!

CodePudding user response:

CH1['pct_change'] = CH1['Area_SqM'].pct_change() 

Would be how I do it normally. Note your first cell is going to be empty (Null)

  • Related