首页 > 解决方案 > 同一周数 Python Pandas 过去 3 年的滚动平均值

问题描述

只要有可用数据(例如 201932),我就会使用 201632、201732 和 201832 的数据的平均值来查找同一周的平均值。示例:2019 年是年份,32 是周数

标签: pythonpandastime-seriesmoving-averagerolling-average

解决方案


通用示例,未完全测试,您可以根据需要进行更新,请原谅语法/编译错误

# 1 load your data here
myYearlyWeekAvgList = [[Calendar, WkNumber, France, 0, 201538], [....]]

# 2 initialize variables here
totalSum = 0
movingAves = 0  # track total for that yr
myYear = currentYear #  input, for e.g. 2018

# 3 start totals & averages here
for i, x in enumerate(myYearlyWeekAvgList, 1):
    T = x[i]            # get each row
    if x == currentYear # for e.g. if yr is 2018, then sum value
        totalSum.append(T[3]) # your revenue in 3rd col I guess

        moving_ave = totalSum/i  # your average for that col

推荐阅读