使用groupby操作计算Pandas百分比

时间:2015-12-06 16:04:14

标签: python pandas group-by

我在pandas中有一个数据框,其格式如下。

 coupon_applied    coupon_type    dish_id     dish_name     dish_price  
0            Yes      Rs 20 off     012     Paneer Biryani         110   

dish_quant_bought  dish_quantity dish_substitute dish_type 
0         50              2             Yes       Veg   

      rder_time            order_id  order_lat    year  
0 2015-12-05 16:30:04.345  order_1   73.955741    2015   

     month       day             time  
0    12       Saturday     16:30:04.345000

我想计算特定时间间隔的菜肴销售率。 菜肴销售率=(dish_quant)/(dish_quant_bought)

我正在关注..

    df_final[(df_final['time'] > datetime.time(16,30)) & (df_final['time'] < datetime.time(16,35))].groupby('dish_name').sum()['dish_quantity']

这让我关注

dish_name
Chicken Biryani         2
Chicken Tikka Masala    2
Mutton Biryani          2
Paneer Biryani          2

但我无法将购买的菜肴数量分成菜量。 怎么做?请帮忙......

1 个答案:

答案 0 :(得分:2)

IIUC你可以很简单地做到:

df = df_final[(df_final['time'] > datetime.time(16,30)) & (df_final['time'] < datetime.time(16,35))].groupby('dish_name').sum()

df['dish_selling_rate'] = df['dish_quantity'] / df['dish_quant_bought']

print df.head(5)