【发布时间】:2018-03-18 14:35:00
【问题描述】:
我有以下一般格式的数据,我想重新采样到 30 天时间序列窗口:
'customer_id','transaction_dt','product','price','units'
1,2004-01-02,thing1,25,47
1,2004-01-17,thing2,150,8
2,2004-01-29,thing2,150,25
3,2017-07-15,thing3,55,17
3,2016-05-12,thing3,55,47
4,2012-02-23,thing2,150,22
4,2009-10-10,thing1,25,12
4,2014-04-04,thing2,150,2
5,2008-07-09,thing2,150,43
我希望 30 天的窗口期从 2014 年 1 月 1 日开始,到 2018 年 12 月 31 日结束。不保证每个客户都会在每个窗口中都有记录。如果客户在一个窗口中有多个交易,那么它会采用价格的加权平均值,对单位求和,然后连接产品名称,从而为每个窗口的每个客户创建一条记录。
到目前为止,我所拥有的是这样的:
wa = lambda x:np.average(x, weights=df.loc[x.index, 'units'])
con = lambda x: '/'.join(x))
agg_funcs = {'customer_id':'first',
'product':'con',
'price':'wa',
'transaction_dt':'first',
'units':'sum'}
df_window = df.groupby(['customer_id', pd.Grouper(freq='30D')]).agg(agg_funcs)
df_window_final = df_window.unstack('customer_id', fill_value=0)
如果有人知道一些更好的方法来解决这个问题(特别是使用就地和/或矢量化方法),我将不胜感激。理想情况下,我还想将窗口开始日期和停止日期作为列添加到行中。
理想的最终输出如下所示:
'customer_id','transaction_dt','product','price','units','window_start_dt','window_end_dt'
1,2004-01-02,thing1/thing2,(weighted average price),(total units),(window_start_dt),(window_end_dt)
2,2004-01-29,thing2,(weighted average price),(total units),(window_start_dt),(window_end_dt)
3,2017-07-15,thing3,(weighted average price),(total units),(window_start_dt),(window_end_dt)
3,2016-05-12,thing3,(weighted average price),(total units),(window_start_dt),(window_end_dt)
4,2012-02-23,thing2,(weighted average price),(total units),(window_start_dt),(window_end_dt)
4,2009-10-10,thing1,(weighted average price),(total units),(window_start_dt),(window_end_dt)
4,2014-04-04,thing2,(weighted average price),(total units),(window_start_dt),(window_end_dt)
5,2008-07-09,thing2,(weighted average price),(total units),(window_start_dt),(window_end_dt)
【问题讨论】:
-
加权平均价格需要一个权重来进行平均。重量是多少。而且,为了不产生歧义,最终结果应该是什么样子,以便决定帮助的人在提交答案之前有一些可比较的东西。
-
对不起,如果混淆了,这应该是根据 groupby 期间窗口中的单位总数计算加权平均价格:wa = lambda x:np.average(x, weights= df.loc[x.index, 'units'])
-
价格权重为 (# of) 个单位。
标签: python pandas time-series resampling pandas-groupby