0

I have a dataset in which there are two index columns. The first one is a MultiIndex and the second is a normal index, but with lots of duplicate entries. I'd like to join those indexes into a second level MultiIndex.

i have something like this:

                         column1|column2|column3|column4|column5|column6|column7
---------------------------------------------------------------------
MultiIndex1|      a      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |      a      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |      a      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |      b      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |      b      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |      b      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00

I'd like my output to look like this:

                         column1|column2|column3|column4|column5|column6|column7
---------------------------------------------------------------------
MultiIndex1|      a      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |             | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |_____________| 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |      b      | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |             | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00
           |             | 1.00 |  2.00 |  3.00 |  4.00 |  5.00 |  6.00 |  7.00

Any help would be immensely appreciated. thank you!

Dataset head

Sorry, don't have enough reputation to post images.

Community
  • 1
  • 1
Omar Omeiri
  • 1,506
  • 1
  • 17
  • 33
  • Each row must have an index label. Quick way to do this is to reset index, then append your current indexes and reorder to get default range index to be the innermost. – Scott Boston Apr 02 '19 at 19:57
  • they each already have a index label, in the picture they are AALR3. I didn't understand your answer. – Omar Omeiri Apr 02 '19 at 20:32
  • Yes, but you wanted AALRE to be grouped right? Then, you'll need a inner index that labels each row. From your picture above. A and B are grouped, but inorder to do that you need to have a inner most index to the AB column. – Scott Boston Apr 02 '19 at 20:37
  • OK, i made it work! Would you like to post an answer so I can mark it as answered? Thanks very much for your help! – Omar Omeiri Apr 02 '19 at 20:55

0 Answers0