pct_change in python – Python Pandas Series pct_change() Function

Pandas Series pct_change() Function:

pct_change in python: By default, the pct change() method of the Pandas Series calculates the percentage change between the current and a prior element. This is helpful for comparing the percentage change in a  time series of elements.

Syntax:

Series.pct_change(periods=1, fill_method='pad', limit=None, freq=None)

Parameters

periods: This is optional. It represents the periods to be shifted for computing the percentage change. 1 is the default.

fill_method: This is optional. It represents how to handle NAs before calculating percent changes. ‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, and None are all valid options it could accept.

pad / ffill: It uses the last valid observation to fill the gap.

backfill / bfill: It uses the next valid observation to fill the gap.

limit: This is optional. It represents the number of consecutive NAs to fill before stopping. None is the default value.

freq: This is optional. A DateOffset, timedelta, or str to indicate increment to use from the time series API (ex: ‘M’ or BDay()). None is the default value.

Return Value:

Python pct_change: The same type as the caller object is returned, but with the percentage change of the element.

Pandas Series pct_change() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above given series
  • Apply pct_change() function on the given series to get the percentage change between the current and the previous element and print the result.
  • Here periods =1 by default
  • Apply pct_change() function on the given series by passing periods as some random number to it to get the percentage change between the current and the previous element with the given periods and print the result.
  • Here periods =2 by default
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([3, 3.2, 10.2, 5.3, 4, 2, 3, 6])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply pct_change() function on the given series to get the
# percentage change between the current and the previous element
# and print the result.
# Here periods =1 by default
print("The percentage change between the current and the previous element:")
print(gvn_series.pct_change())
print()
# Apply pct_change() function on the given series by passing periods as some 
# random number to it to get the percentage change between the current 
# and the previous element with the given periods and print the result.
# Here periods =2 by default
print("The percentage change between the current and the previous element with periods=2:")
print(gvn_series.pct_change(periods=2))

Output:

The given series is:
0     3.0
1     3.2
2    10.2
3     5.3
4     4.0
5     2.0
6     3.0
7     6.0
dtype: float64

The percentage change between the current and the previous element:
0         NaN
1    0.066667
2    2.187500
3   -0.480392
4   -0.245283
5   -0.500000
6    0.500000
7    1.000000
dtype: float64

The percentage change between the current and the previous element with periods=2:
0         NaN
1         NaN
2    2.400000
3    0.656250
4   -0.607843
5   -0.622642
6   -0.250000
7    2.000000
dtype: float64

Example2

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Apply pct_change() function on the student_marks column of the dataframe to get the percentage change between the current and the previous element of the student_marks column and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply pct_change() function on the student_marks column of the 
# dataframe to get the percentage change between the current and the previous element
# of the student_marks column and print the result.
print("The percentage change between the current and the previous element dataframethe student_marks:")
print(data_frme['student_marks'].pct_change())

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The percentage change between the current and the previous element dataframethe student_marks:
virat          NaN
nick     -0.562500
jessy    -0.285714
sindhu    2.600000
Name: student_marks, dtype: float64