user234568
user234568

Reputation: 815

How to use Pivot table in python(3.x) for this problem

I've the following columns as below(interested columns only).

  Ship Mode   : Product Category    :  Product Sub-Category  : Sales
  Express Air :  Office             :  Paper                 : 500
  Express Air :  Technology         :  Paper                 : 200
  Express Air :  Office             :  Pen                   : 500
  Regular Air :  Office             :  Art Pen               : 200
  Regular Air :  Technology         :  Art Pen               : 200
  Regular Air :  Technology         :  Paper                 : 200

How to use pivot table so that it will yield like below

  Ship Mode     Product Sub-Category  Total Sales(for each category)
  Express Air - Paper                  700
              - Pen                    500
  Regular Air - Art Pen                400
              - Paper                  200

I used this statement,but apparently it isn't working. The Sales column appears to be irrelevant to the indexes as it shows total values of other individual columns.

  a1.pivot_table(index = ['Ship Mode','Product Sub-Category'], columns = 'Sales',aggfunc=sum )

Upvotes: 0

Views: 33

Answers (1)

W Barreto
W Barreto

Reputation: 458

Try adding "[]" to the "Sales" column.

a1_pivot = a1.pivot_table(index = ['Ship Mode','Product Sub-Category'], columns=['Sales'],aggfunc=sum)

If it doesn't work, try using "values" instead of "column".

a1.pivot_table(index = ['Ship Mode','Product Sub-Category'],values=['Sales'],aggfunc=sum)

Upvotes: 1

Related Questions