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)
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
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.
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 = Infinte
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.
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.
- Python | pandas.map()
- Python | Pandas Series.agg()
- Python | Pandas dataframe.mean()
- Python | Pandas dataframe.max()
- Python | Pandas Series.dt.tz
- Python | Pandas.CategoricalDtype()
- Python | Pandas dataframe.div()
- Python | Pandas Index.min()
- Python | Pandas Index.max()
- Python | Pandas.Categorical()
- Python | Pandas Index.where
- Python | Pandas dataframe.get()
- Python | Pandas Index.all()
- Python | Pandas Index.any()
- Python | Pandas dataframe.mad()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.