0

I had done this before, but I can't seem to find the way to do it again:

dfs=

month                      charges          pl instrument_type        total
                                                                          
01JUL2019-31JUL2019        1542.4266    2205.000             CDS    3747.4266    
01JUL2019-31JUL2019       -5670.1898       0.000              EQ   -5670.1898    
01JUL2019-31JUL2019       25342.2627 -112535.000              FO  -87192.7373    
01OCT2020-31OCT2020       24769.4206  438663.500              FO  463432.9206
01OCT2020-31OCT2020        1821.6821   -2602.500             CDS    -780.8179
01OCT2020-31OCT2020       -3267.2467   57404.043              EQ   54136.7963

I want to combine the month column to a level show the dfs as:

Expected output:

dfs=

month                      charges          pl instrument_type        total
                                                                          
01JUL2019-31JUL2019        1542.4266    2205.000             CDS    3747.4266    
                          -5670.1898       0.000              EQ   -5670.1898    
                           25342.2627 -112535.000              FO  -87192.7373    
01OCT2020-31OCT2020       24769.4206  438663.500              FO  463432.9206
                            1821.6821   -2602.500             CDS    -780.8179
                            -3267.2467   57404.043              EQ   54136.7963

I assumed this would be done by: dfs.set_index(['month','iunstrument_type'])

But it doesn't result in the desired output. I tried set_levels which didn't work either.

Sid
  • 3,749
  • 7
  • 29
  • 62
  • the "merged" display only works when the indices are sorted contiguously – mozway Jun 28 '22 at 14:45
  • 1
    Hi, apart from the typo, your code works fine. Perhaps this is what you're looking for? https://stackoverflow.com/questions/54299267/why-does-my-multi-index-dataframe-have-duplicate-values-for-indices – Laurent Jul 03 '22 at 16:13

0 Answers0