如果 2020 年日期的预订值小于 25,则将其替换为 pandas 中去年 7 天的平均值

If date of year 2020 have booking value less than 25 than replace it with last year 7 days value average in pandas

我正在处理 pandas 数据框,如果平均 7 天小于 25,我必须替换列预订值示例 x = 22-03-2020 预订值 3,我必须替换它

x = average(before 3 days + last year same date + after 3 days/7))

替换后

我有很多值要替换,我可以手动替换,但我需要一些快捷方式。

您可以将 rollingcenter=True 一起使用,但需要天数的所有值,因此添加 Series.asfreq,然后将一年添加到 DatetimeIndex,因为闰年​​是2 月底为聚合重复添加了 2020 mean,掩码的最后设置值:

df = pd.DataFrame({'date':pd.date_range('2019-03-19', periods=8),
                 'Booking':[92,109,144,109,122,76,78, 3]}) 
df.loc[7, 'date'] = pd.to_datetime('2020-03-22')

df = df.set_index('date')

s = df['Booking'].asfreq('d').rolling(7, center=True).mean()
s.index = s.index + pd.offsets.DateOffset(years=1)
s = s.mean(level=0)
print (s)
date
2020-03-19           NaN
2020-03-20           NaN
2020-03-21           NaN
2020-03-22    104.285714
2020-03-23           NaN
   
2021-03-18           NaN
2021-03-19           NaN
2021-03-20           NaN
2021-03-21           NaN
2021-03-22           NaN
Name: Booking, Length: 369, dtype: float64

mask = df['Booking'].lt(25)

df.loc[mask, 'Booking'] = s
print (df)
               Booking
date                  
2019-03-19   92.000000
2019-03-20  109.000000
2019-03-21  144.000000
2019-03-22  109.000000
2019-03-23  122.000000
2019-03-24   76.000000
2019-03-25   78.000000
2020-03-22  104.285714