To use the diff()
function in pandas, you need to have a DataFrame object. This function calculates the difference between consecutive values in a column or row of your DataFrame.
Here's an example of how to use the diff()
function in pandas:
main.py244 chars12 lines
Output:
main.py72 chars7 lines
As you can see, the diff()
function has calculated the difference between each consecutive value in column 'A' and added the results to a new column named 'diff'.
Note that the first row of the 'diff' column has a NaN (Not a Number) value since there is no previous value to calculate the difference from. You can choose to drop the NaN values if they are not relevant to your analysis.
Remember to import the pandas library before using the diff()
function.
gistlibby LogSnag