Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dataframe

How to obtain the difference in elements of DataFrame objects

Onyejiaku Theophilus Chidalu

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 an int which represents the number of rows or columns to shift for calculating the difference.
  • axis: This represents the name for the row ( designated as 0 or 'index') or the column (designated as 1 or 'columns') axis.

Note: Here, Axis represents the row and column of the DataFrame. Axis with a value of 0 indicate the axes running vertically downwards across a row, while a value of 1 indicates 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 library
import pandas as pd

# creating a dataframe
df = pd.DataFrame([[5,10,4,15,3],
                   [1,7,5,9,0.5],
                   [3,11,13,14,12]],
                   columns=list('ABCDE'))
# printing the dataframe
print(df)

# obtaining the differences vertically across rows
print(df.diff(axis="index"))

# obtaining the ifferences horizontally over columns
print(df.diff(axis="columns"))
Implementing the diff() function

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 (axis 0). 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 (axis 1). We print the result to the console.

RELATED TAGS

dataframe

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring