**Pandas**

*chevron_left*

**Basic and Descriptive Statistics**

# Pandas DataFrame | diff method

*schedule*Aug 11, 2023

*toc*Table of Contents

*expand_more*

**mathematics behind data science**with 100+ top-tier guides

Start your free 7-days trial now!

Pandas `DataFrame.diff(~)`

method returns a new DataFrame where each value represents the difference between the value and the value of the previous row or column.

# Parameters

1. `periods`

link | `int`

| `optional`

If `periods=3`

, then instead of taking the difference of the previous row/column, the difference is computed using the `3`

rd row/column before. Both positive and negative integers are allowed. By default, `periods=1`

.

2. `axis`

| `int`

or `string`

| `optional`

Whether or not the take the difference of the previous row or column:

Axis | Description |
---|---|

| Take the difference between the previous row value in the same column. |

| Take the difference between the previous column value in the same row. |

By default, `axis=0`

.

# Return Value

A new `DataFrame`

.

# Examples

## Basic usage

Consider the following DataFrame:

```
df = pd.DataFrame({"A":[3,4,6],"B":[1,4,9]})df
A B0 3 11 4 42 6 9
```

Computing the difference between each value and its previous value in the same column:

```
df.diff()
A B0 NaN NaN1 1.0 3.02 2.0 5.0
```

Notice how the first row is `NaN`

- this is because there is no previous row with which to compute the difference.

## Specifying periods

To compute the difference between each value and the value that is `2`

rows prior in the same column:

```
df.diff(periods=2)
A B0 NaN NaN1 NaN NaN2 3.0 8.0
```

`periods`

also accepts a negative integer:

```
df.diff(periods=-1)
A B0 -1.0 -3.01 -2.0 -5.02 NaN NaN
```

Here, instead of taking the difference between the previous value, we take the difference between the next value.