Set all columns as index, or convert a dataframe to a multiindex series











up vote
1
down vote

favorite












I have the following dataframe:



from    to  x     ratea
1 2 0.4 10
1 4 0.6 80
1 5 0.2 10
2 3 0.2 10
2 4 0.4 10
2 6 0.3 10
3 5 0.2 10
4 6 0.3 10


My goal is to have something like the following:



from    to      
1 2 x 0.4
1 2 ratea 10
1 4 x 0.6
1 4 ratea 80
1 5 x 0.2
1 5 ratea 10
2 3 x 0.2
2 3 ratea 10
2 4 x 0.4
2 4 ratea 10
2 6 x 0.3
2 6 ratea 10
3 5 x 0.2
3 5 ratea 10
4 6 x 0.3
4 6 ratea 10


How would I do this conversion?



PS unstack() did not help either (or maybe I did not use it probably).










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have the following dataframe:



    from    to  x     ratea
    1 2 0.4 10
    1 4 0.6 80
    1 5 0.2 10
    2 3 0.2 10
    2 4 0.4 10
    2 6 0.3 10
    3 5 0.2 10
    4 6 0.3 10


    My goal is to have something like the following:



    from    to      
    1 2 x 0.4
    1 2 ratea 10
    1 4 x 0.6
    1 4 ratea 80
    1 5 x 0.2
    1 5 ratea 10
    2 3 x 0.2
    2 3 ratea 10
    2 4 x 0.4
    2 4 ratea 10
    2 6 x 0.3
    2 6 ratea 10
    3 5 x 0.2
    3 5 ratea 10
    4 6 x 0.3
    4 6 ratea 10


    How would I do this conversion?



    PS unstack() did not help either (or maybe I did not use it probably).










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the following dataframe:



      from    to  x     ratea
      1 2 0.4 10
      1 4 0.6 80
      1 5 0.2 10
      2 3 0.2 10
      2 4 0.4 10
      2 6 0.3 10
      3 5 0.2 10
      4 6 0.3 10


      My goal is to have something like the following:



      from    to      
      1 2 x 0.4
      1 2 ratea 10
      1 4 x 0.6
      1 4 ratea 80
      1 5 x 0.2
      1 5 ratea 10
      2 3 x 0.2
      2 3 ratea 10
      2 4 x 0.4
      2 4 ratea 10
      2 6 x 0.3
      2 6 ratea 10
      3 5 x 0.2
      3 5 ratea 10
      4 6 x 0.3
      4 6 ratea 10


      How would I do this conversion?



      PS unstack() did not help either (or maybe I did not use it probably).










      share|improve this question













      I have the following dataframe:



      from    to  x     ratea
      1 2 0.4 10
      1 4 0.6 80
      1 5 0.2 10
      2 3 0.2 10
      2 4 0.4 10
      2 6 0.3 10
      3 5 0.2 10
      4 6 0.3 10


      My goal is to have something like the following:



      from    to      
      1 2 x 0.4
      1 2 ratea 10
      1 4 x 0.6
      1 4 ratea 80
      1 5 x 0.2
      1 5 ratea 10
      2 3 x 0.2
      2 3 ratea 10
      2 4 x 0.4
      2 4 ratea 10
      2 6 x 0.3
      2 6 ratea 10
      3 5 x 0.2
      3 5 ratea 10
      4 6 x 0.3
      4 6 ratea 10


      How would I do this conversion?



      PS unstack() did not help either (or maybe I did not use it probably).







      python python-3.x pandas dataframe series






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 7:58









      Ali

      826




      826
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Use set_index with stack with rename_axis for set new index names:



          s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
          print (s)

          from to val
          1 2 x 0.4
          ratea 10.0
          4 x 0.6
          ratea 80.0
          5 x 0.2
          ratea 10.0
          2 3 x 0.2
          ratea 10.0
          4 x 0.4
          ratea 10.0
          6 x 0.3
          ratea 10.0
          3 5 x 0.2
          ratea 10.0
          4 6 x 0.3
          ratea 10.0
          dtype: float64





          share|improve this answer























          • Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
            – Ali
            Nov 11 at 8:03








          • 1




            @Ali - You are welcome!
            – jezrael
            Nov 11 at 8:03










          • Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
            – Ali
            Nov 11 at 21:28






          • 1




            @Ali - check edited answer.
            – jezrael
            Nov 12 at 15:51











          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',
          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%2f53246848%2fset-all-columns-as-index-or-convert-a-dataframe-to-a-multiindex-series%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








          up vote
          1
          down vote



          accepted










          Use set_index with stack with rename_axis for set new index names:



          s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
          print (s)

          from to val
          1 2 x 0.4
          ratea 10.0
          4 x 0.6
          ratea 80.0
          5 x 0.2
          ratea 10.0
          2 3 x 0.2
          ratea 10.0
          4 x 0.4
          ratea 10.0
          6 x 0.3
          ratea 10.0
          3 5 x 0.2
          ratea 10.0
          4 6 x 0.3
          ratea 10.0
          dtype: float64





          share|improve this answer























          • Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
            – Ali
            Nov 11 at 8:03








          • 1




            @Ali - You are welcome!
            – jezrael
            Nov 11 at 8:03










          • Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
            – Ali
            Nov 11 at 21:28






          • 1




            @Ali - check edited answer.
            – jezrael
            Nov 12 at 15:51















          up vote
          1
          down vote



          accepted










          Use set_index with stack with rename_axis for set new index names:



          s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
          print (s)

          from to val
          1 2 x 0.4
          ratea 10.0
          4 x 0.6
          ratea 80.0
          5 x 0.2
          ratea 10.0
          2 3 x 0.2
          ratea 10.0
          4 x 0.4
          ratea 10.0
          6 x 0.3
          ratea 10.0
          3 5 x 0.2
          ratea 10.0
          4 6 x 0.3
          ratea 10.0
          dtype: float64





          share|improve this answer























          • Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
            – Ali
            Nov 11 at 8:03








          • 1




            @Ali - You are welcome!
            – jezrael
            Nov 11 at 8:03










          • Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
            – Ali
            Nov 11 at 21:28






          • 1




            @Ali - check edited answer.
            – jezrael
            Nov 12 at 15:51













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Use set_index with stack with rename_axis for set new index names:



          s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
          print (s)

          from to val
          1 2 x 0.4
          ratea 10.0
          4 x 0.6
          ratea 80.0
          5 x 0.2
          ratea 10.0
          2 3 x 0.2
          ratea 10.0
          4 x 0.4
          ratea 10.0
          6 x 0.3
          ratea 10.0
          3 5 x 0.2
          ratea 10.0
          4 6 x 0.3
          ratea 10.0
          dtype: float64





          share|improve this answer














          Use set_index with stack with rename_axis for set new index names:



          s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
          print (s)

          from to val
          1 2 x 0.4
          ratea 10.0
          4 x 0.6
          ratea 80.0
          5 x 0.2
          ratea 10.0
          2 3 x 0.2
          ratea 10.0
          4 x 0.4
          ratea 10.0
          6 x 0.3
          ratea 10.0
          3 5 x 0.2
          ratea 10.0
          4 6 x 0.3
          ratea 10.0
          dtype: float64






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 15:50

























          answered Nov 11 at 7:59









          jezrael

          311k21246322




          311k21246322












          • Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
            – Ali
            Nov 11 at 8:03








          • 1




            @Ali - You are welcome!
            – jezrael
            Nov 11 at 8:03










          • Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
            – Ali
            Nov 11 at 21:28






          • 1




            @Ali - check edited answer.
            – jezrael
            Nov 12 at 15:51


















          • Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
            – Ali
            Nov 11 at 8:03








          • 1




            @Ali - You are welcome!
            – jezrael
            Nov 11 at 8:03










          • Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
            – Ali
            Nov 11 at 21:28






          • 1




            @Ali - check edited answer.
            – jezrael
            Nov 12 at 15:51
















          Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
          – Ali
          Nov 11 at 8:03






          Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
          – Ali
          Nov 11 at 8:03






          1




          1




          @Ali - You are welcome!
          – jezrael
          Nov 11 at 8:03




          @Ali - You are welcome!
          – jezrael
          Nov 11 at 8:03












          Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
          – Ali
          Nov 11 at 21:28




          Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
          – Ali
          Nov 11 at 21:28




          1




          1




          @Ali - check edited answer.
          – jezrael
          Nov 12 at 15:51




          @Ali - check edited answer.
          – jezrael
          Nov 12 at 15:51


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53246848%2fset-all-columns-as-index-or-convert-a-dataframe-to-a-multiindex-series%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

          Full-time equivalent

          Bicuculline

          さくらももこ