我有一个看起来像这样的Dataframe
TOTAL_GMV2012-04-04 999999999.0 470510.11 9999999.0 557351.02 999999999.0 454225.78 2012-04-259999999.0 527932.46 999999999.0 556741.18 2022-05-09999999999.0 524571.93 999999999.0 547195.66 999999999.0 112423.49
假设WEEK
是索引,您可以:
>>> df.TOTAL_GMV.rolling(4).sum()
WEEK
2022-04-04 NaN
2022-04-11 NaN
2022-04-18 NaN
2022-04-25 2010019.37
2022-05-02 2096250.44
2022-05-09 2063471.35
2022-05-16 2156441.23
2022-05-23 1740932.26
Name: TOTAL_GMV, dtype: float64
将其添加到df中,
df['TOTAL_GMV'] = df.TOTAL_GMV.rolling(4).sum()
(如果不是索引,将其更改为df.set_index('WEEK').TOTAL_GMV.rolling(4).sum()
)