Сначала преобразуйте столбец в to_datetime
, затем sort_values
и последний groupby
с join
:
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y')
df = (df.sort_values(['customerid','Age','Date'])
.groupby(['customerid','Age'])['category']
.agg(', '.join)
.reset_index())
print (df)
customerid Age category
0 1 10 Electronics, Clothing
1 2 25 Grocery, Clothing