Python 熊猫:根据过去4周,获取一周中一天的平均销售额
我累坏了,我需要累赘的智慧 我想创建一个简单的销售平均模型,该模型基于过去使用熊猫的一周中的某一天 我的意思是,该模型基于过去4个周一预测下周一的销售额。此外,如果过去没有足够的周数,只需对现有周数进行平均 我的时间序列df示例:Python 熊猫:根据过去4周,获取一周中一天的平均销售额,python,pandas,time-series,Python,Pandas,Time Series,我累坏了,我需要累赘的智慧 我想创建一个简单的销售平均模型,该模型基于过去使用熊猫的一周中的某一天 我的意思是,该模型基于过去4个周一预测下周一的销售额。此外,如果过去没有足够的周数,只需对现有周数进行平均 我的时间序列df示例: weekDay weekNum Sales Date 2013-01-01 2 1 22 2013-01-02 3 1 33 2013-01-03 4 1 44 2013-01-
weekDay weekNum Sales
Date
2013-01-01 2 1 22
2013-01-02 3 1 33
2013-01-03 4 1 44
2013-01-04 5 1 55
2013-01-05 6 1 66
2013-01-06 7 2 76
2013-01-07 1 2 23
2013-01-08 2 2 55
2013-01-09 3 2 34
2013-01-10 4 2 43
2013-01-11 5 2 34
2013-01-12 6 2 53
2013-01-13 7 3 52
2013-01-14 1 3 41
2013-01-15 2 3 31
2013-01-16 3 3 31
2013-01-17 4 3 42
2013-01-18 5 3 23
2013-01-19 6 3 41
2013-01-20 7 4 31
2013-01-21 1 4 31
2013-01-22 2 4 31
2013-01-23 3 4 43
2013-01-24 4 4 53
2013-01-25 5 4 32
2013-01-26 6 4 12
2013-01-27 7 5 41
2013-01-28 1 5 41
2013-01-29 2 5 12
2013-01-30 3 5 76
2013-01-31 4 5 43
2013-02-01 5 5 32
2013-02-02 6 5 54
2013-02-03 7 6 43
2013-02-04 1 6 43
2013-02-05 2 6 12
2013-02-06 3 6 12
2013-02-07 4 6 43
2013-02-08 5 6 22
2013-02-09 6 6 12
我所尝试的:
ts.resample('D').rolling(28).mean()["Sales"]
ts.resample('W-MON').rolling(1).mean()["Sales"]
但这没用 好的,对不起,我想我现在明白你在找什么了:
res = df.join(df.groupby('weekDay').rolling('28d').mean(), rsuffix='_avg')
因此现在res
包含:
为了使其与我们开始使用的优秀ol数据帧相似:
res.drop(['weekDay', 'weekDay_avg', 'weekNum_avg'], axis=1, inplace=True)
res.reset_index(inplace=True)
res.set_index('date', inplace=True)
res.sort_index(inplace=True)
现在看起来是这样的:
希望这次真的有帮助(:Hello stack!为什么没有帮助?你能提供实际输出与预期输出吗?谢谢你的回答!但我不明白你如何计算第一周的平均值…应该是NaN,因为我们没有第一周之前的历史数据。你比我更了解我:)@帕迪:看看新的解决方案,这绝对是正确的解决方案!谢谢你,我检查了你的代码,在我看来,它就像一个魔术
res.drop(['weekDay', 'weekDay_avg', 'weekNum_avg'], axis=1, inplace=True)
res.reset_index(inplace=True)
res.set_index('date', inplace=True)
res.sort_index(inplace=True)
weekDay weekNum Sales Sales_avg
date
2013-01-01 2 1 22 22.000000
2013-01-02 3 1 33 33.000000
2013-01-03 4 1 44 44.000000
2013-01-04 5 1 55 55.000000
2013-01-05 6 1 66 66.000000
2013-01-06 7 2 76 76.000000
2013-01-07 1 2 23 23.000000
2013-01-08 2 2 55 38.500000
2013-01-09 3 2 34 33.500000
2013-01-10 4 2 43 43.500000
2013-01-11 5 2 34 44.500000
2013-01-12 6 2 53 59.500000
2013-01-13 7 3 52 64.000000
2013-01-14 1 3 41 32.000000
2013-01-15 2 3 31 36.000000
2013-01-16 3 3 31 32.666667
2013-01-17 4 3 42 43.000000
2013-01-18 5 3 23 37.333333
2013-01-19 6 3 41 53.333333
2013-01-20 7 4 31 53.000000
2013-01-21 1 4 31 31.666667
2013-01-22 2 4 31 34.750000
2013-01-23 3 4 43 35.250000
2013-01-24 4 4 53 45.500000
2013-01-25 5 4 32 36.000000
2013-01-26 6 4 12 43.000000
2013-01-27 7 5 41 50.000000
2013-01-28 1 5 41 34.000000
2013-01-29 2 5 12 32.250000
2013-01-30 3 5 76 46.000000
2013-01-31 4 5 43 45.250000
2013-02-01 5 5 32 30.250000
2013-02-02 6 5 54 40.000000
2013-02-03 7 6 43 41.750000
2013-02-04 1 6 43 39.000000
2013-02-05 2 6 12 21.500000
2013-02-06 3 6 12 40.500000
2013-02-07 4 6 43 45.250000
2013-02-08 5 6 22 27.250000
2013-02-09 6 6 12 29.750000