• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas pct_change() to compute percent change across columns/rows

Pandas pct_change() to compute percent change across columns/rows

May 18, 2021 by cmdlinetips

In this tutorial, we will see how to compute percent change for values in each column. Pandas’ pct_change() function will compute percent change for each value in a column when compared to the previous element in the column by default. Another way to think is

Computes the percentage change from the immediately previous row

Pandas’ pct_change() function is extremely handy for comparing the percentage of change in a time series data.

Pandas Compute Percent Change
Pandas pct_change()

First, let us load Pandas library and create some toy time series data.

import pandas as pd

Let us create a dataframe with the top tech companies earnings over the last four years. We have the earnings per company as lists.

year=[2017, 2018, 2019, 2020]
facebook =[15934000000, 22112000000, 18485000000, 29146000000]
google= [12662000000, 30736000000, 34343000000, 40269000000]
microsoft= [25489000000, 16571000000, 39240000000, 44281000000]

We can create a Pandas dataframe from these four lists.

df = pd.DataFrame({"facebook":facebook,
                   "google": google,
                   "microsoft": microsoft},
                 index=year)

In our toy time series data we have three columns (companies) and the year as index.

df

        facebook	google	microsoft
2017	15934000000	12662000000	25489000000
2018	22112000000	30736000000	16571000000
2019	18485000000	34343000000	39240000000
2020	29146000000	40269000000	44281000000

Pandas pct_change() function to compute percent change

We can use pct_change() function and compute percentage of change in revenues for each company comparing to previous year.

df.pct_change()

You can see that the first row is NaN as there is nothing before that. And for the rest of the rows we have percent change in earnings with respect to previous year.

        facebook	google	microsoft
2017	NaN	NaN	NaN
2018	0.387724	1.427421	-0.349876
2019	-0.164029	0.117354	1.367992
2020	0.576738	0.172553	0.128466

By default, pct_change() function computes using rows.

df.pct_change(axis='rows')

We can also compute percent change with respect of columns using the argument axis=”columns”. In this example, since the percent change with respect to column does not make sense, we transpose the dataframe before using pct_change().

df.T.pct_change(axis="columns")

              2017	2018	2019	2020
facebook	NaN	0.387724	-0.164029	0.576738
google	NaN	1.427421	0.117354	0.172553
microsoft	NaN	-0.349876	1.367992	0.128466

We can also specify which rows we want to use to compute percent change using “period” argument. For example, to compute the percent change with respect to two years or rows before, we use “period=2” argument. Here we basically compare earnings from year 2019 to 2017 and 2020 to 2020. Because of this we have NaN in the forst two rows. This argument is extremely useful for comparing quarterly earnings/revenue change.

f.pct_change(periods=2)
facebook	google	microsoft
2017	NaN	NaN	NaN
2018	NaN	NaN	NaN
2019	0.160098	1.712289	0.539488
2020	0.318108	0.310157	1.672198

Compute Percent change with missing data with pct_change()

Another useful feature of pct_change() function is that it can handle missing data. Let us create a list with missing values.

google= [12662, 30736, None, 40269]
year=[2017, 2018, 2019, 2020]
facebook =[15934, 22112, 18485, 29146]
microsoft= [25489, 16571, 39240, 44281]

We can create a dataframe using the list with missing values.

df = pd.DataFrame({"facebook":facebook,
                   "google": google,
                   "microsoft": microsoft},
                 index=year)

We can see that Google column has a single missing value.

df

	facebook	google	microsoft
2017	15934	12662.0	25489
2018	22112	30736.0	16571
2019	18485	NaN	39240
2020	29146	40269.0	44281

By default, pct_change() deals with missing data and using the method “bfill”, that uses “next valid observation to fill gap”

df.pct_change()
	facebook	google	microsoft
2017	NaN	NaN	NaN
2018	0.387724	1.427421	-0.349876
2019	-0.164029	0.000000	1.367992
2020	0.576738	0.310157	0.128466

The argument to specify fill method is fill_method.

df.pct_change(fill_method="bfill")
	facebook	google	microsoft
2017	NaN	NaN	NaN
2018	0.387724	1.427421	-0.349876
2019	-0.164029	0.310157	1.367992
2020	0.576738	0.000000	0.128466

Another way to fill the missing values is to use the forward fill using fill_mehtod=”ffill”.

df.pct_change(fill_method="ffill")

	facebook	google	microsoft
2017	NaN	NaN	NaN
2018	0.387724	1.427421	-0.349876
2019	-0.164029	0.000000	1.367992
2020	0.576738	0.310157	0.128466

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailPandas filter(): Select Columns and Rows by Labels in a Dataframe Default ThumbnailHow To Delete Rows in Pandas Dataframe Default ThumbnailHow to Compute Summary Statistics Across Multiple Columns in R Default ThumbnailHow to Filter a Pandas Dataframe Based on Null Values of a Column?

Filed Under: Pandas 101, Pandas pct_change, Python, Python Tips Tagged With: Pandas pct_change

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version