Pandas DataFrame Pct_change Method

PandasPandasBeginner
Practice Now

Introduction

The pct_change() method in the Pandas DataFrame calculates the percent change in the DataFrame between the current and previous element. It is useful for analyzing data and calculating differences in sales, month-to-month or year-to-year.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/conditional_selection("`Conditional Selection`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/conditional_selection -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/lists -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/tuples -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/dictionaries -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/importing_modules -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/numerical_computing -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/data_analysis -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} python/build_in_functions -.-> lab-68688{{"`Pandas DataFrame Pct_change Method`"}} end

Calculate the Percentage Change in a Pandas DataFrame

To calculate the percent change in a Pandas DataFrame, follow these steps:

  1. Import the pandas library.
import pandas as pd
  1. Create a DataFrame with a time series index and desired data.
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2, 751.23, 852.21], 'Tea': [700.21, 695.21, 726.21], 'Pepper': [900.14, 8254.1, 455.27]}, index=Values)
  1. Print the original DataFrame.
print("----------The dataset is----------")
print(df)
  1. Use the pct_change() method to calculate the percentage change in the DataFrame.
print("-------percentage change in the dataset-------")
print(df.pct_change())

Calculate the Percentage Change along the Column Axis

To calculate the percent change along the column axis, modify the code in step 1 as follows:

  1. Import the pandas library.
import pandas as pd
  1. Create a DataFrame with a time series index and desired data.
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2, 751.23, 852.21], 'Tea': [700.21, 695.21, 726.21], 'Pepper': [900.14, 8254.1, 455.27]}, index=Values)
  1. Print the original DataFrame.
print("----------The dataset is----------")
print(df)
  1. Use the pct_change() method with axis=1 to calculate the percentage change along the column axis.
print("-------percentage change in the dataset-------")
print(df.pct_change(axis=1))

Calculate the Percentage Change with a Specified Period

To calculate the percent change with a specified period, modify the code in step 1 as follows:

  1. Import the pandas library.
import pandas as pd
  1. Create a DataFrame with a time series index and desired data.
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2, 751.23, 852.21], 'Tea': [700.21, 695.21, 726.21], 'Pepper': [900.14, 8254.1, 455.27]}, index=Values)
  1. Print the original DataFrame.
print("----------The dataset is----------")
print(df)
  1. Use the pct_change() method with periods=2 to calculate the percentage change with a specified period.
print("-------percentage change in the dataset-------")
print(df.pct_change(periods=2))

Handle Missing Values before Calculation

To handle missing values before calculating the percent change, modify the code in step 1 as follows:

  1. Import the pandas library.
import pandas as pd
  1. Create a DataFrame with a time series index and desired data.
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2, 751.23, 852.21], 'Tea': [700.21, 695.21, 726.21], 'Pepper': [900.14, 8254.1, 455.27]}, index=Values)
  1. Print the original DataFrame.
print("----------The dataset is----------")
print(df)
  1. Use the pct_change() method with fill_method='ffill' to handle missing values before calculation.
print("-------percentage change in the dataset-------")
print(df.pct_change(fill_method='ffill'))

Summary

The pct_change() method in the Pandas DataFrame calculates the percent change between the current and previous element. It can be used to analyze data and calculate differences, and it has parameters to handle missing values and specify a period for calculation. By following the steps in this tutorial, you can use the pct_change() method effectively in your data analysis tasks.

Other Pandas Tutorials you may like