python - 非相邻对之间的 pct_change

标签 python pandas

我正在尝试计算 Pandas 系列中的 pct_change,但不是以常规方式计算相邻元素之间的变化,而是在相隔两个位置的元素内计算变化。

例如:

pd.Series([1, 2, 3, 4, 5, 6, 7])

0    1
1    2
2    3
3    4
4    5
5    6
6    7
dtype: int64

比较 1425364 以及 7(步幅为 3)。

最佳答案

pd.Series.pct_change中使用periods参数:

s.pct_change(periods=3)

输出:

0     NaN
1     NaN
2     NaN
3    3.00
4    1.50
5    1.00
6    0.75
dtype: float64

关于python - 非相邻对之间的 pct_change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56467918/

相关文章:

python - 在python和mysql中插入特定日期范围内的csv数据

python - Cosmos DB 最大文件大小限制 1.5MB

python - 导入错误 : cannot import name SignedJwtAssertionCredentials

python - 计算 pandas groupby 对象中某个组具有特定值的次数

python - 将 pandas df 表转换或 reshape 为向量并重命名列

python - Pandas:如何将多个模式(OR)与 np.where 匹配

python - 使用 MultiIndex 过滤

python - Python 中的联系人跟踪 - 使用时间序列

python - 重命名非常大的 CSV 数据文件的列

python - 将带有元素日期标签的年度财政数据元组 Munge 转换为 Python Pandas 中的时间序列