我可以在python中对不同时间的值求和吗?



我有一个看起来像这样的Dataframe

TOTAL_GMV2012-04-04999999999.0470510.119999999.0557351.02999999999.0454225.782012-04-259999999.0527932.46999999999.0556741.182022-05-09999999999.0524571.93999999999.0547195.66999999999.0112423.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())

相关内容

  • 没有找到相关文章

最新更新