13. Computational tools
Computational tools
Statistical Functions
Percent Change
Series
, DataFrame
, and Panel
all have a method pct_change
to compute the percent change over a given number of periods (using fill_method
to fill NA/null values before computing the percent change).
In [1]: ser = pd.Series(np.random.randn(8)) In [2]: ser.pct_change() Out[2]: 0 NaN 1 -1.602976 2 4.334938 3 -0.247456 4 -2.067345 5 -1.142903 6 -1.688214 7 -9.759729 dtype: float64
In [3]: df = pd.DataFrame(np.random.randn(10, 4)) In [4]: df.pct_change(periods=3) Out[4]: 0 1 2 3 0 NaN NaN NaN NaN 1 NaN NaN NaN NaN 2 NaN NaN NaN NaN 3 -0.218320 -1