gpt4 book ai didi

python - 滚动窗口 Pandas

转载 作者:行者123 更新时间:2023-12-05 07:46:36 25 4
gpt4 key购买 nike

我需要根据我的时间序列创建一个数据集,其中包含由滚动、重叠窗口制成的样本。也就是说,以特定窗口大小和特定步长拆分我的数据框。

如何使用 Pandas 做到这一点?我看到有一个滚动窗口,但它用于对窗口中的值执行一些聚合(例如计算滚动平均值)。我只对隔离这些重叠的窗口感兴趣。怎么做?

所以输出将是这样的数据帧:

1, a
2, b
3, c
4, d
5, e
6, f
7, g

对于窗口大小 3 和步骤 2,输出将是:

1, a
2, b
3, c

3, c
4, d
5, e

5, e
6, f
7, g

明确一点,我知道如何为此编写一个函数,只是想检查 Pandas 中是否已经有可用的东西。

最佳答案

我认为没有任何 pandas 函数可以帮助您。一个简单的实现是:

A = pd.DataFrame(index=range(1,10), 
data=['a','b','c','d','e','f','g','h','i'],
columns=['letters'])

step = 2
size = 3
n_examples = len(A)
dataframes = []
k=0

while(k * step + size < n_examples):
dataframes += [A.loc[k * step:k * step + size]]
k+=1

print(dataframes)

输出:

[  letters
1 a
2 b
3 c, letters
2 b
3 c
4 d
5 e, letters
4 d
5 e
6 f
7 g]

关于python - 滚动窗口 Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40635435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com