Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Anyway to reorder pivot table columns in Python?

Suppose I have a pivot table like this:

import pandas as pd

d = {'Col_A': [1,2,3,3,3,4,9,9,10,11], 
     'Col_B': ['A','K','E','E','H','A','J','A','L','A'],
     'Value1':[648,654,234,873,248,45,67,94,180,120],
     'Value2':[180,120,35,654,789,34,567,21,235,83]
    }

df = pd.DataFrame(data=d)
df_pvt = pd.pivot_table(df,values=['Value1','Value2'], index='Col_A', columns='Col_B', aggfunc=np.sum).fillna(0)
df_pvt

enter image description here

What I want to achieve is to set an order for Col_B (Highlighted) so that the outputs of Value1 and Value2 would display in this order E, J, A, K, L, H.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Convert col_B to a categorical before reshaping:

(df.astype({'Col_B' : pd.CategoricalDtype(['E', 'J', 'A', 'K', 'L', 'H'], ordered = True)})
   .pivot_table(values=['Value1','Value2'], 
                index='Col_A', 
                columns='Col_B', 
                aggfunc=np.sum)
)
      Value1                         Value2
Col_B      E   J    A    K    L    H      E    J    A    K    L    H
Col_A
1          0   0  648    0    0    0      0    0  180    0    0    0
2          0   0    0  654    0    0      0    0    0  120    0    0
3       1107   0    0    0    0  248    689    0    0    0    0  789
4          0   0   45    0    0    0      0    0   34    0    0    0
9          0  67   94    0    0    0      0  567   21    0    0    0
10         0   0    0    0  180    0      0    0    0    0  235    0
11         0   0  120    0    0    0      0    0   83    0    0    0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading