Skip to content
Advertisement

pandas cumsum on lag-differenced dataframe

Say I have a pd.DataFrame() that I differenced with .diff(5), which works like “new number at idx i = (number at idx i) – (number at idx i-5)”

JavaScript

Now I want to undo this operation using the first 5 entries of example_df, and using df_diff.

If i had done .diff(1), I would simply use .cumsum(). But how can I achieve that it only sums up every 5th value?

My desired output is a df with the following values:

JavaScript

Advertisement

Answer

you could shift the column, add them and fill nans:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement