简体   繁体   中英

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 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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM