Skip to content
Advertisement

pandas .diff() but use first cell as difference between last cell in prior column

say that i have a df in the following format:

JavaScript

and i would like to get the difference of the 2020 column by using df['delta'] = df['2020'].diff()

this will obviously return NaN for the first value in the column. how can i make it so that it automatically interprets that diff as the difference between the FIRST value of 2020 and the LAST value of 2019?

Advertisement

Answer

If you want only for 2020:

JavaScript

Prints:

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