How to obtain the difference in elements of DataFrame objects
Overview
The diff() function is used to obtain the discrete difference in the element of a DataFrame compared to another element of the same DataFrame.
Syntax
The diff() function has the following syntax:
DataFrame.diff(periods=1, axis=0)
Syntax for the diff() function in Pandas
Parameters
The diff() function takes the following parameter values:
periods: This takes anintwhich represents the number of rows or columns to shift for calculating the difference.axis: This represents the name for the row ( designated as0or'index') or the column (designated as1or'columns') axis.
Note: Here, Axis represents the row and column of the DataFrame. Axis with a value of
0indicate the axes running vertically downwards across a row, while a value of1indicates the axes running horizontally across a column.
Return value
The diff() function returns a DataFrame holding the first differences of the elements of the DataFrame.
Code example
Let's look at the code below:
# A code to illustrate the diff() function in Pandas# importing the pandas libraryimport pandas as pd# creating a dataframedf = pd.DataFrame([[5,10,4,15,3],[1,7,5,9,0.5],[3,11,13,14,12]],columns=list('ABCDE'))# printing the dataframeprint(df)# obtaining the differences vertically across rowsprint(df.diff(axis="index"))# obtaining the ifferences horizontally over columnsprint(df.diff(axis="columns"))
Code explanation
- Line 4: We import the pandas library.
- Lines 7 to 10: We create a DataFrame,
df. - Line 12: We print the DataFrame,
df. - Line 15: We use the
diff()function to obtain the differences in values of the elements of the DataFrame running downwards across the rows (axis0). We print the result to the console. - Line 18: We use the
diff()function to obtain the differences in values of the elements of the DataFrame running horizontally across columns (axis1). We print the result to the console.