简体   繁体   English

熊猫:滚动意味着仅使用基于另一列的最后更新

[英]Pandas: Rolling mean using only the last update based on another column

I would like to perform a rolling mean while the mean excludes duplicates found in another column.我想执行滚动平均值,而平均值不包括在另一列中找到的重复项。 Let me provide an example dataframe:让我提供一个示例数据框:

Date            Warehose       Value
10-01-1998      London          10
13-01-1998      London          13
15-01-1998      New York        37
12-02-1998      London          21
20-02-1998      New York        39
21-02-1998      New York        17

In this example, let's say I like to perform 30-day rolling mean of Value but taking into account only the last update of the Warehouse location.在此示例中,假设我喜欢执行 30 天滚动Value ,但仅考虑仓库位置的最后一次更新。 The resulting dataframe is expected to be:生成的数据框预计为:

 Date         Value     Rolling_Mean
02-01-1998      10           10
05-01-1998      13           13
15-01-1998      37           20
12-02-1998      21           29           
20-02-1998      39           30 
21-02-1998      17           19

The data I have is relatively big so as efficient as possible is appreciated.我拥有的数据相对较大,因此尽可能高效。

It's a bit tricky.这有点棘手。 As rolling.apply works on Series only and you need both "Wharehose" and "Value" to perform the computation, you need to access the complete dataframe using a function (and a "global" variable, which is not super clean IMO):由于rolling.apply仅适用于 Series 并且您需要“Warehose”和“Value”来执行计算,您需要使用函数(和“全局”变量,这不是超级干净的 IMO)访问完整的数据帧:

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
df2 = df.set_index('Date')

def agg(s):
    return (df2.loc[s.index]
               .drop_duplicates(subset='Warehose', keep='last')
               ['Value'].mean()
           )

df['Rolling_Mean'] = (df.sort_values(by='Date')
                        .rolling('30d', on='Date')
                        ['Value']
                        .apply(agg, raw=False)
                      )

output:输出:

        Date  Warehose  Value  Rolling_Mean
0 1998-01-10    London     10          10.0
1 1998-01-13    London     13          13.0
2 1998-01-15  New York     37          25.0
3 1998-02-12    London     21          29.0
4 1998-02-20  New York     39          30.0
5 1998-02-21  New York     17          19.0

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

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