Groupby
It occurs so many times that I have to record it down.
dataframe before:
date date_block_num shop_id item_id item_price item_cnt_day
0 2013-01-02 0 59 22154 999.00 1.0
1 2013-01-03 0 25 2552 899.00 1.0
2 2013-01-05 0 25 2552 899.00 -1.0
3 2013-01-06 0 25 2554 1709.05 1.0
4 2013-01-15 0 25 2555 1099.00 1.0
5 2013-01-10 0 25 2564 349.00 1.0
monthly_sales=sales.groupby(["date_block_num","shop_id","item_id"])[
"date","item_price","item_cnt_day"].agg({"date":["min",'max'],"item_price":"mean","item_cnt_day":"sum"})
dataframe after:
date item_price item_cnt_day
min max mean sum
date_block_num shop_id item_id
0 0 32 2013-01-03 2013-01-31 221.0 6.0
33 2013-01-03 2013-01-28 347.0 3.0
35 2013-01-31 2013-01-31 247.0 1.0
43 2013-01-31 2013-01-31 221.0 1.0
51 2013-01-13 2013-01-31 128.5 2.0