Change order of pandas.MultiIndex





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















My DataFrame has the following MultiIndex:



 MultiIndex(levels=[[False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True]],
labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1]],
names=['M016105', 'M743110', 'N237000', 'N239000', 'N241000', 'P010014', 'Z050001', 'Z050002', 'Z050005'])


The HTML rendering looks like this:



enter image description here



The list of labels in the header (names) has automatically been sorted alphabetically. However, I would like to determine the order in which they are displayed by passing a list. How can this be done?



I assume that just assigning a different list to names breaks the data as the table values are not automatically ordered differently.










share|improve this question































    1















    My DataFrame has the following MultiIndex:



     MultiIndex(levels=[[False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True]],
    labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 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, 1, 0, 1, 1, 1, 1]],
    names=['M016105', 'M743110', 'N237000', 'N239000', 'N241000', 'P010014', 'Z050001', 'Z050002', 'Z050005'])


    The HTML rendering looks like this:



    enter image description here



    The list of labels in the header (names) has automatically been sorted alphabetically. However, I would like to determine the order in which they are displayed by passing a list. How can this be done?



    I assume that just assigning a different list to names breaks the data as the table values are not automatically ordered differently.










    share|improve this question



























      1












      1








      1


      1






      My DataFrame has the following MultiIndex:



       MultiIndex(levels=[[False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True]],
      labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1]],
      names=['M016105', 'M743110', 'N237000', 'N239000', 'N241000', 'P010014', 'Z050001', 'Z050002', 'Z050005'])


      The HTML rendering looks like this:



      enter image description here



      The list of labels in the header (names) has automatically been sorted alphabetically. However, I would like to determine the order in which they are displayed by passing a list. How can this be done?



      I assume that just assigning a different list to names breaks the data as the table values are not automatically ordered differently.










      share|improve this question
















      My DataFrame has the following MultiIndex:



       MultiIndex(levels=[[False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True], [False, True]],
      labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0,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0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1]],
      names=['M016105', 'M743110', 'N237000', 'N239000', 'N241000', 'P010014', 'Z050001', 'Z050002', 'Z050005'])


      The HTML rendering looks like this:



      enter image description here



      The list of labels in the header (names) has automatically been sorted alphabetically. However, I would like to determine the order in which they are displayed by passing a list. How can this be done?



      I assume that just assigning a different list to names breaks the data as the table values are not automatically ordered differently.







      python pandas dataframe indexing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 14:54







      clstaudt

















      asked Jan 4 at 14:32









      clstaudtclstaudt

      6,3092395167




      6,3092395167
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Use DataFrame.reorder_levels:



          df = pd.DataFrame({
          'A':list('abcdef'),
          'B':[4,5,4,5,5,4],
          'C':[7,8,9,4,2,3],
          'D':[1,3,5,7,1,0],
          'E':[5,3,6,9,2,4],
          'F':list('aaabbb')
          }).set_index(['A','B','C','D'])


          df = df.reorder_levels(['B','C','A','D']).sort_index()
          print (df)
          E F
          B C A D
          4 3 f 0 4 b
          7 a 1 5 a
          9 c 5 6 a
          5 2 e 1 2 b
          4 d 7 9 b
          8 b 3 3 a





          share|improve this answer


























          • Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

            – clstaudt
            Jan 4 at 14:56













          • @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

            – jezrael
            Jan 4 at 14:57











          • Sure, MultiIndex is of course a normal index in most ways.

            – clstaudt
            Jan 4 at 14:59











          • @clstaudt - Thank you for edit, also add sorted output to answer.

            – jezrael
            Jan 4 at 15:01












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040923%2fchange-order-of-pandas-multiindex%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Use DataFrame.reorder_levels:



          df = pd.DataFrame({
          'A':list('abcdef'),
          'B':[4,5,4,5,5,4],
          'C':[7,8,9,4,2,3],
          'D':[1,3,5,7,1,0],
          'E':[5,3,6,9,2,4],
          'F':list('aaabbb')
          }).set_index(['A','B','C','D'])


          df = df.reorder_levels(['B','C','A','D']).sort_index()
          print (df)
          E F
          B C A D
          4 3 f 0 4 b
          7 a 1 5 a
          9 c 5 6 a
          5 2 e 1 2 b
          4 d 7 9 b
          8 b 3 3 a





          share|improve this answer


























          • Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

            – clstaudt
            Jan 4 at 14:56













          • @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

            – jezrael
            Jan 4 at 14:57











          • Sure, MultiIndex is of course a normal index in most ways.

            – clstaudt
            Jan 4 at 14:59











          • @clstaudt - Thank you for edit, also add sorted output to answer.

            – jezrael
            Jan 4 at 15:01
















          2














          Use DataFrame.reorder_levels:



          df = pd.DataFrame({
          'A':list('abcdef'),
          'B':[4,5,4,5,5,4],
          'C':[7,8,9,4,2,3],
          'D':[1,3,5,7,1,0],
          'E':[5,3,6,9,2,4],
          'F':list('aaabbb')
          }).set_index(['A','B','C','D'])


          df = df.reorder_levels(['B','C','A','D']).sort_index()
          print (df)
          E F
          B C A D
          4 3 f 0 4 b
          7 a 1 5 a
          9 c 5 6 a
          5 2 e 1 2 b
          4 d 7 9 b
          8 b 3 3 a





          share|improve this answer


























          • Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

            – clstaudt
            Jan 4 at 14:56













          • @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

            – jezrael
            Jan 4 at 14:57











          • Sure, MultiIndex is of course a normal index in most ways.

            – clstaudt
            Jan 4 at 14:59











          • @clstaudt - Thank you for edit, also add sorted output to answer.

            – jezrael
            Jan 4 at 15:01














          2












          2








          2







          Use DataFrame.reorder_levels:



          df = pd.DataFrame({
          'A':list('abcdef'),
          'B':[4,5,4,5,5,4],
          'C':[7,8,9,4,2,3],
          'D':[1,3,5,7,1,0],
          'E':[5,3,6,9,2,4],
          'F':list('aaabbb')
          }).set_index(['A','B','C','D'])


          df = df.reorder_levels(['B','C','A','D']).sort_index()
          print (df)
          E F
          B C A D
          4 3 f 0 4 b
          7 a 1 5 a
          9 c 5 6 a
          5 2 e 1 2 b
          4 d 7 9 b
          8 b 3 3 a





          share|improve this answer















          Use DataFrame.reorder_levels:



          df = pd.DataFrame({
          'A':list('abcdef'),
          'B':[4,5,4,5,5,4],
          'C':[7,8,9,4,2,3],
          'D':[1,3,5,7,1,0],
          'E':[5,3,6,9,2,4],
          'F':list('aaabbb')
          }).set_index(['A','B','C','D'])


          df = df.reorder_levels(['B','C','A','D']).sort_index()
          print (df)
          E F
          B C A D
          4 3 f 0 4 b
          7 a 1 5 a
          9 c 5 6 a
          5 2 e 1 2 b
          4 d 7 9 b
          8 b 3 3 a






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 15:01

























          answered Jan 4 at 14:36









          jezraeljezrael

          360k26327407




          360k26327407













          • Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

            – clstaudt
            Jan 4 at 14:56













          • @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

            – jezrael
            Jan 4 at 14:57











          • Sure, MultiIndex is of course a normal index in most ways.

            – clstaudt
            Jan 4 at 14:59











          • @clstaudt - Thank you for edit, also add sorted output to answer.

            – jezrael
            Jan 4 at 15:01



















          • Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

            – clstaudt
            Jan 4 at 14:56













          • @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

            – jezrael
            Jan 4 at 14:57











          • Sure, MultiIndex is of course a normal index in most ways.

            – clstaudt
            Jan 4 at 14:59











          • @clstaudt - Thank you for edit, also add sorted output to answer.

            – jezrael
            Jan 4 at 15:01

















          Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

          – clstaudt
          Jan 4 at 14:56







          Thanks, that does what I asked, but I forgot to add one requirement: I want the True/False entries in the MultiIndex to remain sorted hierarchically - as shown in the screenshot above. reorder_levels keeps the order of rows, but I don't care about the order of rows.

          – clstaudt
          Jan 4 at 14:56















          @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

          – jezrael
          Jan 4 at 14:57





          @clstaudt - So need df = df.reorder_levels(['B','C','A','D']).sort_index() ?

          – jezrael
          Jan 4 at 14:57













          Sure, MultiIndex is of course a normal index in most ways.

          – clstaudt
          Jan 4 at 14:59





          Sure, MultiIndex is of course a normal index in most ways.

          – clstaudt
          Jan 4 at 14:59













          @clstaudt - Thank you for edit, also add sorted output to answer.

          – jezrael
          Jan 4 at 15:01





          @clstaudt - Thank you for edit, also add sorted output to answer.

          – jezrael
          Jan 4 at 15:01




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54040923%2fchange-order-of-pandas-multiindex%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'