Reshaping Pandas DataFrame: switch columns to indices and repeated values as columns












1















I've had a really tough time figuring out how to reshape this DataFrame. Sorry about the wording of the question, this problem seems a bit specific.



I have data on several countries along with a column of 6 repeating features and the year this data was recorded. It looks something like this (minus some features and columns):



   Country        Feature           2005    2006    2007    2008    2009

0 Afghanistan Age Dependency 99.0 99.5 100.0 100.2 100.1
1 Afghanistan Birth Rate 44.9 43.9 42.8 41.6 40.3
2 Afghanistan Death Rate 10.7 10.4 10.1 9.8 9.5
3 Albania Age Dependency 53.5 52.2 50.9 49.7 48.7
4 Albania Birth Rate 12.3 11.9 11.6 11.5 11.6
5 Albania Death Rate 5.95 6.13 6.32 6.51 6.68


There doesn't seem to be any way to make pivot_table() work in this situation and I'm having trouble finding what other steps I can take to make it look how I want:



                       Age Dependency    Birth Rate    Death Rate

Afghanistan 2005 99.0 44.9 10.7
2006 99.5 43.9 10.4
2007 100.0 42.8 10.1
2008 100.2 41.6 9.8
2009 100.1 40.3 9.5

Albania 2005 53.5 12.3 5.95
2006 52.2 11.9 6.13
2007 50.9 11.6 6.32
2008 49.7 11.5 6.51
2009 48.7 11.6 6.68


Where the unique values of the 'Feature' column each become a column and the year columns each become part of a multiIndex with the country. Any help is appreciated, thank you!



EDIT: I checked the "duplicate" but I don't see how that question is the same as this one. How would I place the repeated values within my feature column as unique columns while at the same time moving the years to become a multi index with the countries? Sorry if I'm just not getting something.










share|improve this question





























    1















    I've had a really tough time figuring out how to reshape this DataFrame. Sorry about the wording of the question, this problem seems a bit specific.



    I have data on several countries along with a column of 6 repeating features and the year this data was recorded. It looks something like this (minus some features and columns):



       Country        Feature           2005    2006    2007    2008    2009

    0 Afghanistan Age Dependency 99.0 99.5 100.0 100.2 100.1
    1 Afghanistan Birth Rate 44.9 43.9 42.8 41.6 40.3
    2 Afghanistan Death Rate 10.7 10.4 10.1 9.8 9.5
    3 Albania Age Dependency 53.5 52.2 50.9 49.7 48.7
    4 Albania Birth Rate 12.3 11.9 11.6 11.5 11.6
    5 Albania Death Rate 5.95 6.13 6.32 6.51 6.68


    There doesn't seem to be any way to make pivot_table() work in this situation and I'm having trouble finding what other steps I can take to make it look how I want:



                           Age Dependency    Birth Rate    Death Rate

    Afghanistan 2005 99.0 44.9 10.7
    2006 99.5 43.9 10.4
    2007 100.0 42.8 10.1
    2008 100.2 41.6 9.8
    2009 100.1 40.3 9.5

    Albania 2005 53.5 12.3 5.95
    2006 52.2 11.9 6.13
    2007 50.9 11.6 6.32
    2008 49.7 11.5 6.51
    2009 48.7 11.6 6.68


    Where the unique values of the 'Feature' column each become a column and the year columns each become part of a multiIndex with the country. Any help is appreciated, thank you!



    EDIT: I checked the "duplicate" but I don't see how that question is the same as this one. How would I place the repeated values within my feature column as unique columns while at the same time moving the years to become a multi index with the countries? Sorry if I'm just not getting something.










    share|improve this question



























      1












      1








      1








      I've had a really tough time figuring out how to reshape this DataFrame. Sorry about the wording of the question, this problem seems a bit specific.



      I have data on several countries along with a column of 6 repeating features and the year this data was recorded. It looks something like this (minus some features and columns):



         Country        Feature           2005    2006    2007    2008    2009

      0 Afghanistan Age Dependency 99.0 99.5 100.0 100.2 100.1
      1 Afghanistan Birth Rate 44.9 43.9 42.8 41.6 40.3
      2 Afghanistan Death Rate 10.7 10.4 10.1 9.8 9.5
      3 Albania Age Dependency 53.5 52.2 50.9 49.7 48.7
      4 Albania Birth Rate 12.3 11.9 11.6 11.5 11.6
      5 Albania Death Rate 5.95 6.13 6.32 6.51 6.68


      There doesn't seem to be any way to make pivot_table() work in this situation and I'm having trouble finding what other steps I can take to make it look how I want:



                             Age Dependency    Birth Rate    Death Rate

      Afghanistan 2005 99.0 44.9 10.7
      2006 99.5 43.9 10.4
      2007 100.0 42.8 10.1
      2008 100.2 41.6 9.8
      2009 100.1 40.3 9.5

      Albania 2005 53.5 12.3 5.95
      2006 52.2 11.9 6.13
      2007 50.9 11.6 6.32
      2008 49.7 11.5 6.51
      2009 48.7 11.6 6.68


      Where the unique values of the 'Feature' column each become a column and the year columns each become part of a multiIndex with the country. Any help is appreciated, thank you!



      EDIT: I checked the "duplicate" but I don't see how that question is the same as this one. How would I place the repeated values within my feature column as unique columns while at the same time moving the years to become a multi index with the countries? Sorry if I'm just not getting something.










      share|improve this question
















      I've had a really tough time figuring out how to reshape this DataFrame. Sorry about the wording of the question, this problem seems a bit specific.



      I have data on several countries along with a column of 6 repeating features and the year this data was recorded. It looks something like this (minus some features and columns):



         Country        Feature           2005    2006    2007    2008    2009

      0 Afghanistan Age Dependency 99.0 99.5 100.0 100.2 100.1
      1 Afghanistan Birth Rate 44.9 43.9 42.8 41.6 40.3
      2 Afghanistan Death Rate 10.7 10.4 10.1 9.8 9.5
      3 Albania Age Dependency 53.5 52.2 50.9 49.7 48.7
      4 Albania Birth Rate 12.3 11.9 11.6 11.5 11.6
      5 Albania Death Rate 5.95 6.13 6.32 6.51 6.68


      There doesn't seem to be any way to make pivot_table() work in this situation and I'm having trouble finding what other steps I can take to make it look how I want:



                             Age Dependency    Birth Rate    Death Rate

      Afghanistan 2005 99.0 44.9 10.7
      2006 99.5 43.9 10.4
      2007 100.0 42.8 10.1
      2008 100.2 41.6 9.8
      2009 100.1 40.3 9.5

      Albania 2005 53.5 12.3 5.95
      2006 52.2 11.9 6.13
      2007 50.9 11.6 6.32
      2008 49.7 11.5 6.51
      2009 48.7 11.6 6.68


      Where the unique values of the 'Feature' column each become a column and the year columns each become part of a multiIndex with the country. Any help is appreciated, thank you!



      EDIT: I checked the "duplicate" but I don't see how that question is the same as this one. How would I place the repeated values within my feature column as unique columns while at the same time moving the years to become a multi index with the countries? Sorry if I'm just not getting something.







      python pandas dataframe pivot-table






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 15:42









      Brian Tompsett - 汤莱恩

      4,2231338101




      4,2231338101










      asked Nov 14 '18 at 12:45









      Adrian HerrmannAdrian Herrmann

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Use melt with reshape by set_index and unstack:



          df = (df.melt(['Country','Feature'], var_name='year')
          .set_index(['Country','year','Feature'])['value']
          .unstack())
          print (df)
          Feature Age Dependency Birth Rate Death Rate
          Country year
          Afghanistan 2005 99.0 44.9 10.70
          2006 99.5 43.9 10.40
          2007 100.0 42.8 10.10
          2008 100.2 41.6 9.80
          2009 100.1 40.3 9.50
          Albania 2005 53.5 12.3 5.95
          2006 52.2 11.9 6.13
          2007 50.9 11.6 6.32
          2008 49.7 11.5 6.51
          2009 48.7 11.6 6.68





          share|improve this answer
























          • Thank you Jezrael, I was having trouble with those extra steps.

            – Adrian Herrmann
            Nov 15 '18 at 6:38











          • @AdrianHerrmann - so sorry about closing, mea culpa :(

            – jezrael
            Nov 15 '18 at 6:39











          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%2f53300559%2freshaping-pandas-dataframe-switch-columns-to-indices-and-repeated-values-as-col%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









          0














          Use melt with reshape by set_index and unstack:



          df = (df.melt(['Country','Feature'], var_name='year')
          .set_index(['Country','year','Feature'])['value']
          .unstack())
          print (df)
          Feature Age Dependency Birth Rate Death Rate
          Country year
          Afghanistan 2005 99.0 44.9 10.70
          2006 99.5 43.9 10.40
          2007 100.0 42.8 10.10
          2008 100.2 41.6 9.80
          2009 100.1 40.3 9.50
          Albania 2005 53.5 12.3 5.95
          2006 52.2 11.9 6.13
          2007 50.9 11.6 6.32
          2008 49.7 11.5 6.51
          2009 48.7 11.6 6.68





          share|improve this answer
























          • Thank you Jezrael, I was having trouble with those extra steps.

            – Adrian Herrmann
            Nov 15 '18 at 6:38











          • @AdrianHerrmann - so sorry about closing, mea culpa :(

            – jezrael
            Nov 15 '18 at 6:39
















          0














          Use melt with reshape by set_index and unstack:



          df = (df.melt(['Country','Feature'], var_name='year')
          .set_index(['Country','year','Feature'])['value']
          .unstack())
          print (df)
          Feature Age Dependency Birth Rate Death Rate
          Country year
          Afghanistan 2005 99.0 44.9 10.70
          2006 99.5 43.9 10.40
          2007 100.0 42.8 10.10
          2008 100.2 41.6 9.80
          2009 100.1 40.3 9.50
          Albania 2005 53.5 12.3 5.95
          2006 52.2 11.9 6.13
          2007 50.9 11.6 6.32
          2008 49.7 11.5 6.51
          2009 48.7 11.6 6.68





          share|improve this answer
























          • Thank you Jezrael, I was having trouble with those extra steps.

            – Adrian Herrmann
            Nov 15 '18 at 6:38











          • @AdrianHerrmann - so sorry about closing, mea culpa :(

            – jezrael
            Nov 15 '18 at 6:39














          0












          0








          0







          Use melt with reshape by set_index and unstack:



          df = (df.melt(['Country','Feature'], var_name='year')
          .set_index(['Country','year','Feature'])['value']
          .unstack())
          print (df)
          Feature Age Dependency Birth Rate Death Rate
          Country year
          Afghanistan 2005 99.0 44.9 10.70
          2006 99.5 43.9 10.40
          2007 100.0 42.8 10.10
          2008 100.2 41.6 9.80
          2009 100.1 40.3 9.50
          Albania 2005 53.5 12.3 5.95
          2006 52.2 11.9 6.13
          2007 50.9 11.6 6.32
          2008 49.7 11.5 6.51
          2009 48.7 11.6 6.68





          share|improve this answer













          Use melt with reshape by set_index and unstack:



          df = (df.melt(['Country','Feature'], var_name='year')
          .set_index(['Country','year','Feature'])['value']
          .unstack())
          print (df)
          Feature Age Dependency Birth Rate Death Rate
          Country year
          Afghanistan 2005 99.0 44.9 10.70
          2006 99.5 43.9 10.40
          2007 100.0 42.8 10.10
          2008 100.2 41.6 9.80
          2009 100.1 40.3 9.50
          Albania 2005 53.5 12.3 5.95
          2006 52.2 11.9 6.13
          2007 50.9 11.6 6.32
          2008 49.7 11.5 6.51
          2009 48.7 11.6 6.68






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:23









          jezraeljezrael

          333k24276352




          333k24276352













          • Thank you Jezrael, I was having trouble with those extra steps.

            – Adrian Herrmann
            Nov 15 '18 at 6:38











          • @AdrianHerrmann - so sorry about closing, mea culpa :(

            – jezrael
            Nov 15 '18 at 6:39



















          • Thank you Jezrael, I was having trouble with those extra steps.

            – Adrian Herrmann
            Nov 15 '18 at 6:38











          • @AdrianHerrmann - so sorry about closing, mea culpa :(

            – jezrael
            Nov 15 '18 at 6:39

















          Thank you Jezrael, I was having trouble with those extra steps.

          – Adrian Herrmann
          Nov 15 '18 at 6:38





          Thank you Jezrael, I was having trouble with those extra steps.

          – Adrian Herrmann
          Nov 15 '18 at 6:38













          @AdrianHerrmann - so sorry about closing, mea culpa :(

          – jezrael
          Nov 15 '18 at 6:39





          @AdrianHerrmann - so sorry about closing, mea culpa :(

          – jezrael
          Nov 15 '18 at 6:39


















          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%2f53300559%2freshaping-pandas-dataframe-switch-columns-to-indices-and-repeated-values-as-col%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python