pandas.DataFrame.pct_change

Percentage change between the current and a prior element.

Computes the percentage change from the immediately previous row by default. This is useful in comparing the percentage of change in a time series of elements.

param periods
int, default 1

Periods to shift for forming percent change.

param fill_method
str, default ‘pad’

How to handle NAs before computing percent changes.

param limit
int, default None

The number of consecutive NAs to fill before stopping.

param freq
DateOffset, timedelta, or offset alias string, optional

Increment to use from time series API (e.g. ‘M’ or BDay()). **kwargs Additional keyword arguments are passed into DataFrame.shift or Series.shift.

return

chg : Series or DataFrame The same type as the calling object.

Limitations

Parameters limit and freq are unsupported.

Examples

Percentage change between the current and a prior element.
import pandas as pd
from numba import njit


@njit
def dataframe_pct_change():
    df = pd.DataFrame({"A": [14, 4, 5, 4, 1, 55],
                       "B": [5, 2, 54, 3, 2, 32],
                       "C": [20, 20, 7, 21, 8, 5],
                       "D": [14, 3, 6, 2, 6, 4]})
    out_df = df.pct_change()

    return out_df


print(dataframe_pct_change())
$ python ./dataframe/dataframe_pct_change.py
           A          B         C         D
0        NaN        NaN       NaN       NaN
1  -0.714286  -0.600000  0.000000 -0.785714
2   0.250000  26.000000 -0.650000  1.000000
3  -0.200000  -0.944444  2.000000 -0.666667
4  -0.750000  -0.333333 -0.619048  2.000000
5  54.000000  15.000000 -0.375000 -0.333333

See also

Series.diff

Compute the difference of two elements in a Series.

DataFrame.diff

Compute the difference of two elements in a DataFrame.

Series.shift

Shift the index by some number of periods.

DataFrame.shift

Shift the index by some number of periods.