pandas 熊猫分组两列并乘以另外两列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17280651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas groupby two columns and multiply two other columns
提问by richie
I have a dataframe grouped by like this;
我有一个这样分组的数据框;
price quantity vat
date brand
20-Jun-13 Reebok 7.0 8 2.2
Adidas 12.0 3 3.8
Campus 2.5 38 4.2
Woodlands 23.0 9 7.2
Boot 3.2 35 3.3
21-Jun-13 Reebok 7.0 6 2.2
Adidas 12.0 23 3.8
Campus 2.5 18 4.2
Woodlands 23.0 29 7.2
Boot 3.2 15 3.3
22-Jun-13 Reebok 5.0 2 3.5
Adidas 10.0 5 2.8
Campus 2.0 50 3.5
Woodlands 25.0 4 6.5
Boot 2.5 10 2.8
How do I groupby 'date'and 'brand'and multiply 'price'and 'quantity'to calculate sales?
我如何分组'date'和'brand'乘以'price'和'quantity'计算销售额?
I've tried;
我试过了;
print data2.groupby(['date','brand'])['price'] * ['quantity']
打印 data2.groupby(['date','brand'])['price'] * ['quantity']
I would like to calculate the total sales by date.
我想按日期计算总销售额。
采纳答案by Andy Hayden
Your data is already grouped by date and brand, so why not just create a new sales column:
您的数据已经按日期和品牌分组,那么为什么不创建一个新的销售列:
df['sales'] = df['price'] * df['quantity']

