Pandas Groupby using multiple criteria on different axis












2














I have a df DataFrame like :



      | A | B | A_ | B_ |COMMON|
--------------------------------
0 | 1 | 3 | 0 | 1 | a |
--------------------------------
1 | 8 | 5 | 4 | 0 | a |
--------------------------------
2 | 3 | 6 | 2 | 4 | b |
--------------------------------
3 | 9 | 9 | 1 | 7 | b |


And I want to group all columns X with X_ for all letters A,B,... (let's say, the group is called X as well), and group as well using COMMON. I would like to apply later function like std() to all the grouped values.



So the result would look like:



COMMON  |  A     |  B     |
---------------------------
a |std(...)|std(...)|
---------------------------
b |std(...)|std(...)|


I have been able to group either one or the other, using df.groupby(['COMMMON']) for one criteria and .groupby(mapping_function, axis=1) for the other one, but how do I use them together?



Another alternative for an intermediate step would be to concatenate individual columns so that I would get:



      | A | B |COMMON|
----------------------
0 | 1 | 3 |a |
---------------------
1 | 8 | 5 |a |
---------------------
2 | 3 | 6 |b |
---------------------
3 | 9 | 9 |b |
---------------------
0 | 0 | 1 |a |
---------------------
1 | 4 | 0 |a |
---------------------
2 | 2 | 4 |b |
---------------------
3 | 1 | 7 |b |


But I also don't know how to do that.
Also as you might see, I don't really care about the index.



Thank you for your help!










share|improve this question



























    2














    I have a df DataFrame like :



          | A | B | A_ | B_ |COMMON|
    --------------------------------
    0 | 1 | 3 | 0 | 1 | a |
    --------------------------------
    1 | 8 | 5 | 4 | 0 | a |
    --------------------------------
    2 | 3 | 6 | 2 | 4 | b |
    --------------------------------
    3 | 9 | 9 | 1 | 7 | b |


    And I want to group all columns X with X_ for all letters A,B,... (let's say, the group is called X as well), and group as well using COMMON. I would like to apply later function like std() to all the grouped values.



    So the result would look like:



    COMMON  |  A     |  B     |
    ---------------------------
    a |std(...)|std(...)|
    ---------------------------
    b |std(...)|std(...)|


    I have been able to group either one or the other, using df.groupby(['COMMMON']) for one criteria and .groupby(mapping_function, axis=1) for the other one, but how do I use them together?



    Another alternative for an intermediate step would be to concatenate individual columns so that I would get:



          | A | B |COMMON|
    ----------------------
    0 | 1 | 3 |a |
    ---------------------
    1 | 8 | 5 |a |
    ---------------------
    2 | 3 | 6 |b |
    ---------------------
    3 | 9 | 9 |b |
    ---------------------
    0 | 0 | 1 |a |
    ---------------------
    1 | 4 | 0 |a |
    ---------------------
    2 | 2 | 4 |b |
    ---------------------
    3 | 1 | 7 |b |


    But I also don't know how to do that.
    Also as you might see, I don't really care about the index.



    Thank you for your help!










    share|improve this question

























      2












      2








      2


      1





      I have a df DataFrame like :



            | A | B | A_ | B_ |COMMON|
      --------------------------------
      0 | 1 | 3 | 0 | 1 | a |
      --------------------------------
      1 | 8 | 5 | 4 | 0 | a |
      --------------------------------
      2 | 3 | 6 | 2 | 4 | b |
      --------------------------------
      3 | 9 | 9 | 1 | 7 | b |


      And I want to group all columns X with X_ for all letters A,B,... (let's say, the group is called X as well), and group as well using COMMON. I would like to apply later function like std() to all the grouped values.



      So the result would look like:



      COMMON  |  A     |  B     |
      ---------------------------
      a |std(...)|std(...)|
      ---------------------------
      b |std(...)|std(...)|


      I have been able to group either one or the other, using df.groupby(['COMMMON']) for one criteria and .groupby(mapping_function, axis=1) for the other one, but how do I use them together?



      Another alternative for an intermediate step would be to concatenate individual columns so that I would get:



            | A | B |COMMON|
      ----------------------
      0 | 1 | 3 |a |
      ---------------------
      1 | 8 | 5 |a |
      ---------------------
      2 | 3 | 6 |b |
      ---------------------
      3 | 9 | 9 |b |
      ---------------------
      0 | 0 | 1 |a |
      ---------------------
      1 | 4 | 0 |a |
      ---------------------
      2 | 2 | 4 |b |
      ---------------------
      3 | 1 | 7 |b |


      But I also don't know how to do that.
      Also as you might see, I don't really care about the index.



      Thank you for your help!










      share|improve this question













      I have a df DataFrame like :



            | A | B | A_ | B_ |COMMON|
      --------------------------------
      0 | 1 | 3 | 0 | 1 | a |
      --------------------------------
      1 | 8 | 5 | 4 | 0 | a |
      --------------------------------
      2 | 3 | 6 | 2 | 4 | b |
      --------------------------------
      3 | 9 | 9 | 1 | 7 | b |


      And I want to group all columns X with X_ for all letters A,B,... (let's say, the group is called X as well), and group as well using COMMON. I would like to apply later function like std() to all the grouped values.



      So the result would look like:



      COMMON  |  A     |  B     |
      ---------------------------
      a |std(...)|std(...)|
      ---------------------------
      b |std(...)|std(...)|


      I have been able to group either one or the other, using df.groupby(['COMMMON']) for one criteria and .groupby(mapping_function, axis=1) for the other one, but how do I use them together?



      Another alternative for an intermediate step would be to concatenate individual columns so that I would get:



            | A | B |COMMON|
      ----------------------
      0 | 1 | 3 |a |
      ---------------------
      1 | 8 | 5 |a |
      ---------------------
      2 | 3 | 6 |b |
      ---------------------
      3 | 9 | 9 |b |
      ---------------------
      0 | 0 | 1 |a |
      ---------------------
      1 | 4 | 0 |a |
      ---------------------
      2 | 2 | 4 |b |
      ---------------------
      3 | 1 | 7 |b |


      But I also don't know how to do that.
      Also as you might see, I don't really care about the index.



      Thank you for your help!







      python pandas pandas-groupby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 14:22









      cduguet

      311214




      311214
























          3 Answers
          3






          active

          oldest

          votes


















          7














          You can reshape first by melt with removing _ from column names (for better performance, because strip only few values) with pivot_table:



          df = (df.rename(columns=lambda x: x.strip('_'))
          .melt('COMMON')
          .pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))

          print (df)
          variable A B
          COMMON
          a 3.593976 2.217356
          b 3.593976 2.081666





          share|improve this answer



















          • 2




            Nice pivot :-) .
            – W-B
            Nov 12 at 14:33



















          5














          IIUC



          df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
          groupby(['COMMON','variable']).value.std().unstack()
          Out[18]:
          variable A B
          COMMON
          a 3.593976 2.217356
          b 3.593976 2.081666





          share|improve this answer





























            2














            Just groupby



            h = lambda x: x[-1][0]

            df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()

            A B
            COMMON
            a 3.593976 2.217356
            b 3.593976 2.081666





            share|improve this answer























              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%2f53264153%2fpandas-groupby-using-multiple-criteria-on-different-axis%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              You can reshape first by melt with removing _ from column names (for better performance, because strip only few values) with pivot_table:



              df = (df.rename(columns=lambda x: x.strip('_'))
              .melt('COMMON')
              .pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))

              print (df)
              variable A B
              COMMON
              a 3.593976 2.217356
              b 3.593976 2.081666





              share|improve this answer



















              • 2




                Nice pivot :-) .
                – W-B
                Nov 12 at 14:33
















              7














              You can reshape first by melt with removing _ from column names (for better performance, because strip only few values) with pivot_table:



              df = (df.rename(columns=lambda x: x.strip('_'))
              .melt('COMMON')
              .pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))

              print (df)
              variable A B
              COMMON
              a 3.593976 2.217356
              b 3.593976 2.081666





              share|improve this answer



















              • 2




                Nice pivot :-) .
                – W-B
                Nov 12 at 14:33














              7












              7








              7






              You can reshape first by melt with removing _ from column names (for better performance, because strip only few values) with pivot_table:



              df = (df.rename(columns=lambda x: x.strip('_'))
              .melt('COMMON')
              .pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))

              print (df)
              variable A B
              COMMON
              a 3.593976 2.217356
              b 3.593976 2.081666





              share|improve this answer














              You can reshape first by melt with removing _ from column names (for better performance, because strip only few values) with pivot_table:



              df = (df.rename(columns=lambda x: x.strip('_'))
              .melt('COMMON')
              .pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))

              print (df)
              variable A B
              COMMON
              a 3.593976 2.217356
              b 3.593976 2.081666






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 at 14:45

























              answered Nov 12 at 14:29









              jezrael

              319k22258336




              319k22258336








              • 2




                Nice pivot :-) .
                – W-B
                Nov 12 at 14:33














              • 2




                Nice pivot :-) .
                – W-B
                Nov 12 at 14:33








              2




              2




              Nice pivot :-) .
              – W-B
              Nov 12 at 14:33




              Nice pivot :-) .
              – W-B
              Nov 12 at 14:33













              5














              IIUC



              df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
              groupby(['COMMON','variable']).value.std().unstack()
              Out[18]:
              variable A B
              COMMON
              a 3.593976 2.217356
              b 3.593976 2.081666





              share|improve this answer


























                5














                IIUC



                df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
                groupby(['COMMON','variable']).value.std().unstack()
                Out[18]:
                variable A B
                COMMON
                a 3.593976 2.217356
                b 3.593976 2.081666





                share|improve this answer
























                  5












                  5








                  5






                  IIUC



                  df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
                  groupby(['COMMON','variable']).value.std().unstack()
                  Out[18]:
                  variable A B
                  COMMON
                  a 3.593976 2.217356
                  b 3.593976 2.081666





                  share|improve this answer












                  IIUC



                  df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
                  groupby(['COMMON','variable']).value.std().unstack()
                  Out[18]:
                  variable A B
                  COMMON
                  a 3.593976 2.217356
                  b 3.593976 2.081666






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 14:30









                  W-B

                  99.7k73163




                  99.7k73163























                      2














                      Just groupby



                      h = lambda x: x[-1][0]

                      df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()

                      A B
                      COMMON
                      a 3.593976 2.217356
                      b 3.593976 2.081666





                      share|improve this answer




























                        2














                        Just groupby



                        h = lambda x: x[-1][0]

                        df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()

                        A B
                        COMMON
                        a 3.593976 2.217356
                        b 3.593976 2.081666





                        share|improve this answer


























                          2












                          2








                          2






                          Just groupby



                          h = lambda x: x[-1][0]

                          df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()

                          A B
                          COMMON
                          a 3.593976 2.217356
                          b 3.593976 2.081666





                          share|improve this answer














                          Just groupby



                          h = lambda x: x[-1][0]

                          df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()

                          A B
                          COMMON
                          a 3.593976 2.217356
                          b 3.593976 2.081666






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 12 at 15:06

























                          answered Nov 12 at 14:56









                          piRSquared

                          151k22143285




                          151k22143285






























                              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%2f53264153%2fpandas-groupby-using-multiple-criteria-on-different-axis%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