Home > OS >  Anyway to reorder pivot table columns in Python?
Anyway to reorder pivot table columns in Python?

Time:12-08

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.

CodePudding user response:

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
  • Related