Pandas pct_change() method is applied on series with numeric data to calculate Percentage change after n number of elements. By default, it calculates percentage change of current element from the previous element. (Current-Previous/Previous) * 100.
First, n(n=period) values are always NaN, since there is no previous value to calculate change.
Syntax: Series.pct_change(periods=1, fill_method=’pad’, limit=None)
Parameters:
periods: Defines gap between current and previous value. Default is 1
fill_method: Defines method used to handle null values
limit: Number of consecutive NaN values to fill before stopping.
Return type: Numeric series with percentage change
Example #1:
In this method, a Series is created from Python list using Pandas Series(). The series doesn’t contain any null value and hence pct_change() method is called directly with default value of period parameter, that is 1.
Python3
# importing pandas module import pandas as pd # importing numpy module import numpy as np # creating list list = [ 10 , 14 , 20 , 25 , 12.5 , 13 , 0 , 50 ] # creating series series = pd.Series( list ) # calling method result = series.pct_change() # display result |
Output:
0 NaN 1 0.400000 2 0.428571 3 0.250000 4 -0.500000 5 0.040000 6 -1.000000 7 inf dtype: float64
As shown in output, first n values are always equal to NaN. Rest of the values are equal to the percentage change in Old values and are stored at the same position as caller series.
Note: Since second last value was 0, the Percentage change is inf. inf stands for infinite.
Using the formula, pct_change= x-0/0 = Infinite
Example #2: Handling Null values
In this example, some null values are also created using Numpy’s np.nan method and passed to the list. ‘bfill‘ is passed to fill_method. bfill stands for Back fill and will fill Null values with values at their very next position.
Python3
# importing pandas module import pandas as pd # importing numpy module import numpy as np # creating list list = [ 10 , np.nan, 14 , 20 , 25 , 12.5 , 13 , 0 , 50 ] # creating series series = pd.Series( list ) # calling method result = series.pct_change(fill_method = 'bfill' ) # display result |
Output:
0 NaN 1 0.400000 2 0.000000 3 0.428571 4 0.250000 5 -0.500000 6 0.040000 7 -1.000000 8 inf dtype: float64
As it can be seen in output, value at position 1 is 40 because NaN was replaced by 14. Hence, (14-10/10) *100 = 40. The very next value is 0 because percentage change in 14 and 14 is 0.