简体   繁体   English

获取pandas数据帧的最后N个工作日的平均值

[英]Get mean of last N weekdays for pandas dataframe

Assume my data is daily counts and has as its index a DateTimeIndex column. 假设我的数据是每日计数,并将DateTimeIndex列作为其索引。 Is there a way to get the average of the past n weekdays? 有没有办法获得过去n个工作日的平均值? For instance, if the date is Sunday August 15th, I'd like to get mean of counts on (sunday august 8th, sunday august 1st, ...). 例如,如果日期是8月15日星期日,我想得到统计数字(星期日8月8日,星期日8月1日,......)。

I started using pandas yesterday, so here's what I've brute forced. 我昨天开始使用大熊猫,所以这就是我的强奸。

# df is a dataframe with an DateTimeIndex
# brute force for count last n weekdays, wherelnwd = last n weekdays
def lnwd(n=1):
    lnwd, tmp = df.shift(7), df.shift(7) # count last weekday
    for i in xrange(n-1):
        tmp = tmp.shift(7)
        lnwd += tmp
    lnwd = lnwd/n  # average
    return lnwd

There has to be a one liner? 必须有一个班轮? Is there a way to use apply() (without passing a function that has a for loop? since n is variable) or some form of groupby ? 有没有办法使用apply() (不传递具有for循环的函数?因为n是可变的)或某种形式的groupby For instance, the way to find the mean of all data on each weekday is: 例如,在每个工作日查找所有数据的平均值的方法是:

df.groupby(lambda x: x.dayofweek).mean() # mean of each MTWHFSS

I think you are looking for a rolling apply (rolling mean in this case)? 我认为你正在寻找滚动申请(在这种情况下滚动均值)? See the docs: http://pandas.pydata.org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments . 请参阅文档: http//pandas.pydata.org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments But then applied for each weekday seperately, this can be achieved by combining rolling_mean with grouping on the weekday with groupby . 但随后在每个工作日分别申请,这可以通过将rolling_mean与工作日的分组与groupby相结合来实现。

This should give somethin like (with a series s ): 这应该给某些东西(有系列s ):

s.groupby(s.index.weekday).transform(lambda x: pd.rolling_mean(x, window=n))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM