I want to class my pivot table according to my other column in Pandas

Question:

I have a data frame similar to mentioned below:

Sample DataFrame

And I need to get this type of table as the result:

a.pivot_table(index=['DT'],columns=['TYPE'], values='AMOUNT',aggfunc=['count','sum','mean']).T

where a is the actual data

enter image description here

I desire to get firstly comes TYPE after count, sum, mean.

Asked By: Emil Memmedsoy

||

Answers:

You can just make use of swaplevel method of the multi indexed data frame as shown below:
enter image description here

a.pivot_table(index=['DT'],columns=['TYPE'], values='AMOUNT',aggfunc=['count','sum','mean']).T.swaplevel(0,1)

And your new data frame looks like this:

enter image description here

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.