import pandas as pd
pd.__version__
from pandas import concat
def handler(grouped):
se = grouped.set_index('Date')['Sale'].sort_index()
return concat(
{
'MeanToDate': se.expanding().mean(),
'MaxToDate': se.expanding().max(),
'SaleCount': se.expanding().count(),
'Sale': se,
'PrevSale': se.shift(1)
},
axis=1
)
from datetime import datetime
df = pd.DataFrame({'Basket':[88,88,88,123,477,477,566],
'Sale':[15,30,16,90,77,57,90],
'Date':[datetime.strptime(ds,'%d/%m/%Y')
for ds in ['3/01/2012','11/02/2012','16/08/2012','18/06/2012',
'19/08/2012','11/12/2012','6/07/2012']]})
new_df = df.groupby('Basket').apply(handler).reset_index()