Pandas DataFrame diff() Method – Be on the Right Side of Change (original) (raw)


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd import numpy as np


The diff() method calculates the difference between a DataFrame element compared with another element in the same DataFrame. The default is the element in the previous row.

The syntax for this method is as follows:

DataFrame.diff(periods=1, axis=0)

Parameter Description
axis If zero (0) or index is selected, apply to each column. Default 0.If one (1) apply to each row.
periods The periods to shift for calculating differences. This parameter accepts negative values.

Code – Example 1

This example reflects the difference in regard to the previous row.

df_teams = pd.DataFrame({'Bruins': [4, 5, 9], 'Oilers': [3, 6, 10], 'Leafs': [2, 7, 11], 'Flames': [1, 8, 12]})

result = df_teams.diff() print(result)

Output

| | Bruins | Oilers | Leafs | Flames | | | -------- | ------ | ----- | ------ | --- | | 0 | NaN | NaN | NaN | NaN | | 1 | 1.0 | 3.0 | 5.0 | 7.0 | | 2 | 4.0 | 4.0 | 4.0 | 4.0 |

Code – Example 2

This example reflects the difference in regard to the previous column.

df_teams = pd.DataFrame({'Bruins': [4, 5, 9], 'Oilers': [3, 6, 10], 'Leafs': [2, 7, 11], 'Flames': [1, 8, 12]})

result = df_teams.diff(axis=1) print(result)

Output

| | Bruins | Oilers | Leafs | Flames | | | -------- | ------ | ----- | ------ | --- | | 0 | NaN | -1 | -1 | -1 | | 1 | NaN | 1 | 1 | 1 | | 2 | NaN | 1 | 1 | 1 |

Code – Example 3

This example reflects the difference in regard to the previous rows.

df_teams = pd.DataFrame({'Bruins': [4, 5, 9], 'Oilers': [3, 6, 10], 'Leafs': [2, 7, 11], 'Flames': [1, 8, 12]})

result = df_teams.diff(periods=1) print(result)

Output

| | Bruins | Oilers | Leafs | Flames | | | -------- | ------ | ----- | ------ | --- | | 0 | NaN | NaN | NaN | NaN | | 1 | 1.0 | 3.0 | 5.0 | 7.0 | | 2 | 4.0 | 4.0 | 4.0 | 4.0 |


More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.