Skip to content
Advertisement

Pandas: Calculate neighbouring differences from a column in dataframe

How can I calculate the differences from neighboured numbers in a dataframe column named 'y' by only using Pandas commands?

Here is an example where I convert the column 'y'first to numpy and then use np.diff.

JavaScript

Advertisement

Answer

You could use diff to find the differences and shift to get the differences align (like in your output):

JavaScript

Output:

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