:파이썬 팬더 : 리샘플링 다변량 시계열 GROUPBY와
'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-01-01 년 12 월 31 일부터 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)
가중 평균 가격과 평균의 무게를 필요로한다. 무게는 얼마입니까? 그리고 모호함이 없도록 최종 결과는 어떻게 보이게되어 도움을주기로 결정한 사람들은 답을 제출하기 전에 비교할 것이 있습니다. – piRSquared
죄송합니다. 혼란 스럽다면 groupby 동안 창에있는 총 단위 수에서 가중 평균 가격을 계산해야합니다. wa = lambda x : np.average (x, weights = df.loc [x.index , 'units']) – Pylander
가격의 무게는 (of) 단위입니다. – Pylander