Search code examples
pythonpandasseries

how to get pandas pct_change result in percents?


I'm using pandas's example to do what I want to do:

>>> s = pd.Series([90, 91, 85])
>>> s
0    90
1    91
2    85
dtype: int64

then the pct_change() is applied to this series:

>>> s.pct_change()
0         NaN
1    0.011111
2   -0.065934
dtype: float64

okay, fair enough, but Percentage Increase = [ (Final Value - Starting Value) / |Starting Value| ] × 100

so the results should actually be [NaN, 1.11111%, -6.59341%]. how would I get this *100 part that the pct_change() didn't run for me?


Solution

  • You can simply multiply the result by 100 to get what you want:

    In [712]: s.pct_change().mul(100)
    Out[712]: 
    0         NaN
    1    1.111111
    2   -6.593407
    dtype: float64
    

    If you want the result to be a list of these values, do this:

    In [714]: l = s.pct_change().mul(100).tolist()
    
    In [715]: l
    Out[715]: [nan, 1.1111111111111072, -6.593406593406592]