Change language

Python | Pandas Series.diff ()

Pandas Series.diff() is used to find differences between items in the same series. The difference is sequential and depends on the period parameter passed to the diff () method.

Syntax: Series.diff (periods = 1)

Parameters:
periods: integer value, subtracts element before / after period from current element. Negative values ​​are also accepted

Return type: Series

Example:
In this example two series are created from Python lists. The diff () method is called for both series, once with a positive period and once with a negative value passed to the period parameter.

# pandas module import

import pandas as pd 

 
# numpy module import

import numpy as np 

 
# create a list

list = [ 15 , 2 , 34 , 12 , 4 , 0 , 9 , 7 ]

 
# create series

series = pd.Series ( list )

 
# method call with period 2

period2 = series.diff ( 2 )

 
# Passing a negative value in the period
# passing period -1

period_1 = series.diff ( - 1 )

 
# display

print (’Diff with period 2 : {}

Diff with period - 1 : {} ’. format (period2, period_1))

Exit:

 Diff with period 2: 0 NaN 1 NaN 2 19.0 3 10.0 4 -30.0 5 -12.0 6 5.0 7 7.0 dtype: float64 Diff with period -1: 0 13.0 1 -32.0 2 22.0 3 8.0 4 4.0 5 -9.0 6 2.0 7 NaN dtype: float64 

Explanation: In the first output with a period of 2 digits The i-th position was subtracted from the (i + 2) th position and stored in the (i + 2) th position. In the second output, the value at the i-th position was subtracted from the values ​​in the (i-1) th position and stored in the (i-1) th position.

Note: Values ​​ First / Last n in the output series are equal to NaN depending on the sign of the period (first if the period is positive, and Last if it is negative, where n — period).

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically