Summing specific columns based on a mapping












0















I have a series which contains a mapping:



serm = pd.Series(
data={'ARD1': 53, 'BUL1': 37,
'BUL2': 37, 'BSR1': 49, 'BTR1': 53, 'CR1': 53,
'CRR1': 53, 'CRE3': 53,'TAB1': 52, 'NEP1': 42, 'HAL1': 42})


which maps the asset id (the index) to an area (the value).
I have the the following dataframe where serm index is the columns names.



data=pd.DataFrame(data={'ARD1': {0: 4.0, 1: 2.0, 2: 2.0, 3: 3.0, 4: 2.0},
'BUL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'BUL2': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'BSR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'BTR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'CR1': {0: 15.0, 1: 13.0, 2: 13.0, 3: 11.0, 4: 13.0},
'CRR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'CRE3': {0: 8.0, 1: 10.0, 2: 9.0, 3: 10.0, 4: 11.0},
'TAB1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'NEP1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'HAL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}})


I would like to sum the columns of data that fall in the same area, according to the mapping of serm. How can I achieve this (the more pandanoic the better)










share|improve this question



























    0















    I have a series which contains a mapping:



    serm = pd.Series(
    data={'ARD1': 53, 'BUL1': 37,
    'BUL2': 37, 'BSR1': 49, 'BTR1': 53, 'CR1': 53,
    'CRR1': 53, 'CRE3': 53,'TAB1': 52, 'NEP1': 42, 'HAL1': 42})


    which maps the asset id (the index) to an area (the value).
    I have the the following dataframe where serm index is the columns names.



    data=pd.DataFrame(data={'ARD1': {0: 4.0, 1: 2.0, 2: 2.0, 3: 3.0, 4: 2.0},
    'BUL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'BUL2': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'BSR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'BTR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'CR1': {0: 15.0, 1: 13.0, 2: 13.0, 3: 11.0, 4: 13.0},
    'CRR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'CRE3': {0: 8.0, 1: 10.0, 2: 9.0, 3: 10.0, 4: 11.0},
    'TAB1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'NEP1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
    'HAL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}})


    I would like to sum the columns of data that fall in the same area, according to the mapping of serm. How can I achieve this (the more pandanoic the better)










    share|improve this question

























      0












      0








      0








      I have a series which contains a mapping:



      serm = pd.Series(
      data={'ARD1': 53, 'BUL1': 37,
      'BUL2': 37, 'BSR1': 49, 'BTR1': 53, 'CR1': 53,
      'CRR1': 53, 'CRE3': 53,'TAB1': 52, 'NEP1': 42, 'HAL1': 42})


      which maps the asset id (the index) to an area (the value).
      I have the the following dataframe where serm index is the columns names.



      data=pd.DataFrame(data={'ARD1': {0: 4.0, 1: 2.0, 2: 2.0, 3: 3.0, 4: 2.0},
      'BUL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'BUL2': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'BSR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'BTR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'CR1': {0: 15.0, 1: 13.0, 2: 13.0, 3: 11.0, 4: 13.0},
      'CRR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'CRE3': {0: 8.0, 1: 10.0, 2: 9.0, 3: 10.0, 4: 11.0},
      'TAB1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'NEP1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'HAL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}})


      I would like to sum the columns of data that fall in the same area, according to the mapping of serm. How can I achieve this (the more pandanoic the better)










      share|improve this question














      I have a series which contains a mapping:



      serm = pd.Series(
      data={'ARD1': 53, 'BUL1': 37,
      'BUL2': 37, 'BSR1': 49, 'BTR1': 53, 'CR1': 53,
      'CRR1': 53, 'CRE3': 53,'TAB1': 52, 'NEP1': 42, 'HAL1': 42})


      which maps the asset id (the index) to an area (the value).
      I have the the following dataframe where serm index is the columns names.



      data=pd.DataFrame(data={'ARD1': {0: 4.0, 1: 2.0, 2: 2.0, 3: 3.0, 4: 2.0},
      'BUL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'BUL2': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'BSR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'BTR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'CR1': {0: 15.0, 1: 13.0, 2: 13.0, 3: 11.0, 4: 13.0},
      'CRR1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'CRE3': {0: 8.0, 1: 10.0, 2: 9.0, 3: 10.0, 4: 11.0},
      'TAB1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'NEP1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
      'HAL1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}})


      I would like to sum the columns of data that fall in the same area, according to the mapping of serm. How can I achieve this (the more pandanoic the better)







      python-3.x pandas pandas-groupby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 17 hours ago









      AliAli

      846




      846
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use Index.map with groupby per columns and aggregate sum:



          df = data.groupby(data.columns.map(serm.get), axis=1).sum()
          print (df)
          37 42 49 52 53
          0 0.0 0.0 0.0 0.0 27.0
          1 0.0 0.0 0.0 0.0 25.0
          2 0.0 0.0 0.0 0.0 24.0
          3 0.0 0.0 0.0 0.0 24.0
          4 0.0 0.0 0.0 0.0 26.0


          Or assign columns back and use sum:



          data.columns = data.columns.map(serm.get)
          df = data.sum(level=0, axis=1)





          share|improve this answer
























          • Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

            – Ali
            8 hours ago











          • @Ali You can check this, but if use last version of pandas get should be omit.

            – jezrael
            6 hours ago











          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%2f54250500%2fsumming-specific-columns-based-on-a-mapping%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









          1














          Use Index.map with groupby per columns and aggregate sum:



          df = data.groupby(data.columns.map(serm.get), axis=1).sum()
          print (df)
          37 42 49 52 53
          0 0.0 0.0 0.0 0.0 27.0
          1 0.0 0.0 0.0 0.0 25.0
          2 0.0 0.0 0.0 0.0 24.0
          3 0.0 0.0 0.0 0.0 24.0
          4 0.0 0.0 0.0 0.0 26.0


          Or assign columns back and use sum:



          data.columns = data.columns.map(serm.get)
          df = data.sum(level=0, axis=1)





          share|improve this answer
























          • Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

            – Ali
            8 hours ago











          • @Ali You can check this, but if use last version of pandas get should be omit.

            – jezrael
            6 hours ago
















          1














          Use Index.map with groupby per columns and aggregate sum:



          df = data.groupby(data.columns.map(serm.get), axis=1).sum()
          print (df)
          37 42 49 52 53
          0 0.0 0.0 0.0 0.0 27.0
          1 0.0 0.0 0.0 0.0 25.0
          2 0.0 0.0 0.0 0.0 24.0
          3 0.0 0.0 0.0 0.0 24.0
          4 0.0 0.0 0.0 0.0 26.0


          Or assign columns back and use sum:



          data.columns = data.columns.map(serm.get)
          df = data.sum(level=0, axis=1)





          share|improve this answer
























          • Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

            – Ali
            8 hours ago











          • @Ali You can check this, but if use last version of pandas get should be omit.

            – jezrael
            6 hours ago














          1












          1








          1







          Use Index.map with groupby per columns and aggregate sum:



          df = data.groupby(data.columns.map(serm.get), axis=1).sum()
          print (df)
          37 42 49 52 53
          0 0.0 0.0 0.0 0.0 27.0
          1 0.0 0.0 0.0 0.0 25.0
          2 0.0 0.0 0.0 0.0 24.0
          3 0.0 0.0 0.0 0.0 24.0
          4 0.0 0.0 0.0 0.0 26.0


          Or assign columns back and use sum:



          data.columns = data.columns.map(serm.get)
          df = data.sum(level=0, axis=1)





          share|improve this answer













          Use Index.map with groupby per columns and aggregate sum:



          df = data.groupby(data.columns.map(serm.get), axis=1).sum()
          print (df)
          37 42 49 52 53
          0 0.0 0.0 0.0 0.0 27.0
          1 0.0 0.0 0.0 0.0 25.0
          2 0.0 0.0 0.0 0.0 24.0
          3 0.0 0.0 0.0 0.0 24.0
          4 0.0 0.0 0.0 0.0 26.0


          Or assign columns back and use sum:



          data.columns = data.columns.map(serm.get)
          df = data.sum(level=0, axis=1)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 17 hours ago









          jezraeljezrael

          326k23268344




          326k23268344













          • Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

            – Ali
            8 hours ago











          • @Ali You can check this, but if use last version of pandas get should be omit.

            – jezrael
            6 hours ago



















          • Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

            – Ali
            8 hours ago











          • @Ali You can check this, but if use last version of pandas get should be omit.

            – jezrael
            6 hours ago

















          Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

          – Ali
          8 hours ago





          Thank you for your prompt answer. Would you mind explaining what get does? I couldn't find a good doc on it. Thank you very much in advance.

          – Ali
          8 hours ago













          @Ali You can check this, but if use last version of pandas get should be omit.

          – jezrael
          6 hours ago





          @Ali You can check this, but if use last version of pandas get should be omit.

          – jezrael
          6 hours ago


















          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%2f54250500%2fsumming-specific-columns-based-on-a-mapping%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

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage