Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas groupby two columns and multiply two other columns

Tags:

python

pandas

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?

I've tried;

print data2.groupby(['date','brand'])['price'] * ['quantity']

I would like to calculate the total sales by date.

like image 755
richie Avatar asked Jun 24 '13 16:06

richie


1 Answers

Your data is already grouped by date and brand, so why not just create a new sales column:

df['sales'] = df['price'] * df['quantity']
like image 93
Andy Hayden Avatar answered Jan 04 '23 06:01

Andy Hayden