**
**python中的pct_change用法:
**
代码:
**
s=pd.Series([1,2,3,4,5])
print(‘periods=1,(当前值-前1值)/前1值’)
print(s.pct_change(periods=1))
print(‘periods=2,(当前值-前2值)/前2值’)
print(s.pct_change(periods=2))
print(‘去掉NaN’)
print(s.pct_change(periods=2).dropna())
结果:
periods=1,(当前值-前1值)/前1值
0 NaN
1 1.000000
2 0.500000
3 0.333333
4 0.250000
dtype: float64
periods=2,(当前值-前2值)/前2值
0 NaN
1 NaN
2 2.000000
3 1.000000
4 0.666667
dtype: float64
去掉NaN
2 2.000000
3 1.000000
4 0.666667
dtype: float64
**
分析:
**
(1)periods=1,(当前值-前1值)/前1值
(1-NaN)/NaN,(2-1)/1,(3-2)/2,(4-3)/3,(5-4)/4
(2)periods=2,(当前值-前2值)/前2值
(1-NaN)/NaN,(2-NaN)/NaN,(3-1)/1,(4-2)/2,(5-3)/3