您可以使用as_strided
:
from numpy.lib.stride_tricks import as_strided window = 3 stride = df['a'].values.strides[0] pd.DataFrame(as_strided(df['a'].values, shape=(len(df) - window + 1, window), strides = (stride,stride)) )
输出:
0 1 2 0 41 42 43 1 42 43 44 2 43 44 45