Python | Pandas Series.diff()
Last Updated :
20 Nov, 2018
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 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: Series
Example:
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.
import pandas as pd
import numpy as np
list = [ 15 , 2 , 34 , 12 , 4 , 0 , 9 , 7 ]
series = pd.Series( list )
period2 = series.diff( 2 )
period_1 = series.diff( - 1 )
print ('Diff with period 2 :\n{}\n\
Diff with period - 1 :\n{}'. format (period2, period_1))
|
Output:
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 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).
Share your thoughts in the comments
Please Login to comment...