【发布时间】:2016-10-08 17:18:48
【问题描述】:
我有一个熊猫系列,一个时间和一个价值。 我想计算每个值之间的变化。 像这样:当前值/先前值。
当我运行这段代码时:
print now.head(n=3)
print before.head(n=3)
delta = now.divide(before)
print delta.iloc[1]
print now.iloc[1] / before.iloc[1]
我得到这个结果:
DateTime
2014-01-08 09:27:00 623.53836
2014-01-08 09:28:00 623.54066
2014-01-08 09:32:00 623.53846
Name: close, dtype: float64
DateTime
2014-01-08 09:26:00 624.01000
2014-01-08 09:27:00 623.53836
2014-01-08 09:28:00 623.54066
Name: close, dtype: float64
1.0
1.00000368863
由于最后两个数字不同,我错过了什么?
现在和之前的系列是同一个系列,只是移动了一个位置。
更新:问题是pandas在分割时匹配的索引。幸运的是,pandas 有一个名为 .pct_change() 的内置函数,它完全符合我的要求。感谢 Steven G. 向我展示了这一点。
【问题讨论】: