2

long time XLS user still new to Python/Pandas.I'm tying to automate a report for some price curves and I can't quite manage to get the Pivot in the format I'm looking for. Thanks in advance for your help!

I have a dataframe in this format:

 data = [['AAA',1,11,1,],['AAA',2,12,2],['AAA',3,13,3],
          ['BBB',1,21,5],['BBB',2,22,6],['BBB',3,23,7],
         ['CCC',1,31,9],['CCC',2,32,10],['CCC',3,33,11]]
            
    df = pd.DataFrame(data, columns = ['Curve', 'Tenor','Price','Change'])
        
    print(df)
    
      Curve  Tenor  Price  Change
    0   AAA      1     11       1
    1   AAA      2     12       2
    2   AAA      3     13       3
    3   BBB      1     21       5
    4   BBB      2     22       6
    5   BBB      3     23       7
    6   CCC      1     31       9
    7   CCC      2     32      10
    8   CCC      3     33      11

I pivoted the df as follows and the result looks like this:

df2 = pd.pivot_table(df, values=['Price','Change'], index='Tenor',columns='Curve', aggfunc=np.mean)

          Change         Price        
    Curve    AAA BBB CCC   AAA BBB CCC
    Tenor                             
    1          1   5   9    11  21  31
    2          2   6  10    12  22  32
    3          3   7  11    13  23  33

I'd like it to change the order of the column grouping such that it looked like

        AAA             BBB             CCC 
Tenor   Price   Change  Price   Change  Price   Change
1        11     1       5       21      31      9
2        12     2       6       22      32      10
3        13     3       7       23      33      11

In XLS you'd just move the fields around physically in the Pivot. I'm sure its just as trivial here too :) but no matter how I google I can't seem to find a solution. Thanks again for your help! Any and all assistance appreciated. Cheers.

1 Answer 1

4

We can swaplevel, sort_index and reindex to reorganize the headers:

df2 = (
    df.pivot_table(values=['Price', 'Change'], index='Tenor', columns='Curve')
        .swaplevel(axis=1)
        .sort_index(level=0, axis=1)
        .reindex(['Price', 'Change'], level=1, axis=1)
        .rename_axis(columns=[None, None])
)

df2:

        AAA          BBB          CCC       
      Price Change Price Change Price Change
Tenor                                       
1        11      1    21      5    31      9
2        12      2    22      6    32     10
3        13      3    23      7    33     11
  1. swaplevel switches the values in level 0 and level 1 (so AAA, BBB, CCC become top level index)
  2. sort_index will lexicographically sort the new top level AAA BBB CCC (we could have alternatively done .reindex(['AAA', 'BBB', 'CCC'], level=0, axis=1) to supply a specific order for that level.)
  3. reindex is needed to put "Price" before "Change" since sorting will not work since "Change" comes alphabetically before "Price"
  4. rename_axis get's rid of the axis name "Curve" which is created when pivoting.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. This worked great! I have a slightly more complex pivot to create a report out of which i'm having a similar problem with. stackoverflow.com/questions/69951324/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.