To make differences of multiple columns in a dataframe, we can use the pandas
library’s DataFrame.diff()
method. This method calculates the first difference of a DataFrame and returns a new DataFrame.
Here is an example of how to make differences of multiple columns in a dataframe:
main.py265 chars12 lines
This will output:
main.py120 chars7 lines
In the output dataframe, you can see that the first row contains NaN because there is no previous row to calculate the difference with. The remaining rows show the differences between the corresponding values of the dataframe.
gistlibby LogSnag