Python | Pandas Series.diff()
Last Updated :
20 Nov, 2018
Improve
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas
Python3 1==
Output:
Series.diff()
is used to find difference between elements of the same series. The difference is sequential and depends on period parameter passed to 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: SeriesExample: In this example, two series are created from Python lists.
diff()
method is called on both series, one time with positive period and one time with negative value passed to period parameter.
# importing pandas module
import pandas as pd
# importing numpy module
import numpy as np
# creating list
list =[15, 2, 34, 12, 4, 0, 9, 7]
# creating series
series = pd.Series(list)
# calling method with period 2
period2 = series.diff(2)
# Passing Negative value to period
# passing period of -1
period_1 = series.diff(-1)
# display
print('Diff with period 2:\n{}\n\
Diff with period -1:\n{}'.format(period2, period_1))
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: float64Explanation: In the first output with period 2, value at ith position were subtracted from (i+2)th position and stored at (i+2)th position. In second output, value at ith position were subtracted from values at (i-1)th position and stored at (i-1)th position. Note: First/Last n values in output series are NaN depending on sign of period.(First if period is positive and Last if negative where n is period).