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.
Calculate the Percentage Change in a Pandas DataFrame
To calculate the percent change in a Pandas DataFrame, follow these steps:
- Import the pandas library.
import pandas as pd
- 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)
- Print the original DataFrame.
print("----------The dataset is----------")
print(df)
- 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:
- Import the pandas library.
import pandas as pd
- 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)
- Print the original DataFrame.
print("----------The dataset is----------")
print(df)
- Use the
pct_change()method withaxis=1to 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:
- Import the pandas library.
import pandas as pd
- 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)
- Print the original DataFrame.
print("----------The dataset is----------")
print(df)
- Use the
pct_change()method withperiods=2to 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:
- Import the pandas library.
import pandas as pd
- 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)
- Print the original DataFrame.
print("----------The dataset is----------")
print(df)
- Use the
pct_change()method withfill_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.