Adding a grand total when using the groupedby function on a DataFrame












1














Suppose I have the following DataFrame



>>> d = {'col1': ['A', 'B', 'A', 'A', 'B'], 'col2': ['q1', 'q2', 'q1', 'q2', 'q1'], 
'col3':[1, 2, 1, 4, 5]}

>>> df = pd.DataFrame(data=d)
>>> df
col1 col2 col3
0 A q1 1
1 B q2 2
2 A q1 1
3 A q2 4
4 B q1 5


Now I would like to group this by col1 and col2, and also add the grand total below
So my result would be:



                   col3
col1 col2
A q1 2
A q2 4
total_A 6
B q1 5
B q2 2
total_B 7


Any ideas on how to add this total value while keeping grouped columns 1 and 2? Preferably not manually since my actual DataFrame is quite a bit larger










share|improve this question





























    1














    Suppose I have the following DataFrame



    >>> d = {'col1': ['A', 'B', 'A', 'A', 'B'], 'col2': ['q1', 'q2', 'q1', 'q2', 'q1'], 
    'col3':[1, 2, 1, 4, 5]}

    >>> df = pd.DataFrame(data=d)
    >>> df
    col1 col2 col3
    0 A q1 1
    1 B q2 2
    2 A q1 1
    3 A q2 4
    4 B q1 5


    Now I would like to group this by col1 and col2, and also add the grand total below
    So my result would be:



                       col3
    col1 col2
    A q1 2
    A q2 4
    total_A 6
    B q1 5
    B q2 2
    total_B 7


    Any ideas on how to add this total value while keeping grouped columns 1 and 2? Preferably not manually since my actual DataFrame is quite a bit larger










    share|improve this question



























      1












      1








      1







      Suppose I have the following DataFrame



      >>> d = {'col1': ['A', 'B', 'A', 'A', 'B'], 'col2': ['q1', 'q2', 'q1', 'q2', 'q1'], 
      'col3':[1, 2, 1, 4, 5]}

      >>> df = pd.DataFrame(data=d)
      >>> df
      col1 col2 col3
      0 A q1 1
      1 B q2 2
      2 A q1 1
      3 A q2 4
      4 B q1 5


      Now I would like to group this by col1 and col2, and also add the grand total below
      So my result would be:



                         col3
      col1 col2
      A q1 2
      A q2 4
      total_A 6
      B q1 5
      B q2 2
      total_B 7


      Any ideas on how to add this total value while keeping grouped columns 1 and 2? Preferably not manually since my actual DataFrame is quite a bit larger










      share|improve this question















      Suppose I have the following DataFrame



      >>> d = {'col1': ['A', 'B', 'A', 'A', 'B'], 'col2': ['q1', 'q2', 'q1', 'q2', 'q1'], 
      'col3':[1, 2, 1, 4, 5]}

      >>> df = pd.DataFrame(data=d)
      >>> df
      col1 col2 col3
      0 A q1 1
      1 B q2 2
      2 A q1 1
      3 A q2 4
      4 B q1 5


      Now I would like to group this by col1 and col2, and also add the grand total below
      So my result would be:



                         col3
      col1 col2
      A q1 2
      A q2 4
      total_A 6
      B q1 5
      B q2 2
      total_B 7


      Any ideas on how to add this total value while keeping grouped columns 1 and 2? Preferably not manually since my actual DataFrame is quite a bit larger







      python pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '18 at 15:32









      coldspeed

      120k19119194




      120k19119194










      asked Dec 27 '18 at 15:31









      Whizkid95

      16010




      16010
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Compute the totals separately and concatenate them together:



          # First step: GroupBy and sum.
          u = df.groupby(['col1', 'col2']).sum()
          # Second step: Compute the totals and set a MultiIndex for easy concatenation.
          v = u.groupby(level=0).sum()
          v.index = pd.MultiIndex.from_arrays([v.index, ['total'] * len(v)])
          # Final step: Concat the intermediate results.
          pd.concat([v, u]).sort_index(level=0, axis=0)

          col3
          col1
          A q1 2
          q2 4
          total 6
          B q1 5
          q2 2
          total 7





          share|improve this answer





























            2














            You can try this also:



            d = df.groupby(['col1','col2']).sum()
            pd.concat([d,
            d.sum(level=0).assign(col2='')
            .rename(index=lambda x: x+' total')
            .set_index('col2', append=True)]).sort_index()


            Output:



                          col3
            col1 col2
            A q1 2
            q2 4
            A total 6
            B q1 5
            q2 2
            B total 7





            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%2f53947377%2fadding-a-grand-total-when-using-the-groupedby-function-on-a-dataframe%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Compute the totals separately and concatenate them together:



              # First step: GroupBy and sum.
              u = df.groupby(['col1', 'col2']).sum()
              # Second step: Compute the totals and set a MultiIndex for easy concatenation.
              v = u.groupby(level=0).sum()
              v.index = pd.MultiIndex.from_arrays([v.index, ['total'] * len(v)])
              # Final step: Concat the intermediate results.
              pd.concat([v, u]).sort_index(level=0, axis=0)

              col3
              col1
              A q1 2
              q2 4
              total 6
              B q1 5
              q2 2
              total 7





              share|improve this answer


























                2














                Compute the totals separately and concatenate them together:



                # First step: GroupBy and sum.
                u = df.groupby(['col1', 'col2']).sum()
                # Second step: Compute the totals and set a MultiIndex for easy concatenation.
                v = u.groupby(level=0).sum()
                v.index = pd.MultiIndex.from_arrays([v.index, ['total'] * len(v)])
                # Final step: Concat the intermediate results.
                pd.concat([v, u]).sort_index(level=0, axis=0)

                col3
                col1
                A q1 2
                q2 4
                total 6
                B q1 5
                q2 2
                total 7





                share|improve this answer
























                  2












                  2








                  2






                  Compute the totals separately and concatenate them together:



                  # First step: GroupBy and sum.
                  u = df.groupby(['col1', 'col2']).sum()
                  # Second step: Compute the totals and set a MultiIndex for easy concatenation.
                  v = u.groupby(level=0).sum()
                  v.index = pd.MultiIndex.from_arrays([v.index, ['total'] * len(v)])
                  # Final step: Concat the intermediate results.
                  pd.concat([v, u]).sort_index(level=0, axis=0)

                  col3
                  col1
                  A q1 2
                  q2 4
                  total 6
                  B q1 5
                  q2 2
                  total 7





                  share|improve this answer












                  Compute the totals separately and concatenate them together:



                  # First step: GroupBy and sum.
                  u = df.groupby(['col1', 'col2']).sum()
                  # Second step: Compute the totals and set a MultiIndex for easy concatenation.
                  v = u.groupby(level=0).sum()
                  v.index = pd.MultiIndex.from_arrays([v.index, ['total'] * len(v)])
                  # Final step: Concat the intermediate results.
                  pd.concat([v, u]).sort_index(level=0, axis=0)

                  col3
                  col1
                  A q1 2
                  q2 4
                  total 6
                  B q1 5
                  q2 2
                  total 7






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 '18 at 15:38









                  coldspeed

                  120k19119194




                  120k19119194

























                      2














                      You can try this also:



                      d = df.groupby(['col1','col2']).sum()
                      pd.concat([d,
                      d.sum(level=0).assign(col2='')
                      .rename(index=lambda x: x+' total')
                      .set_index('col2', append=True)]).sort_index()


                      Output:



                                    col3
                      col1 col2
                      A q1 2
                      q2 4
                      A total 6
                      B q1 5
                      q2 2
                      B total 7





                      share|improve this answer


























                        2














                        You can try this also:



                        d = df.groupby(['col1','col2']).sum()
                        pd.concat([d,
                        d.sum(level=0).assign(col2='')
                        .rename(index=lambda x: x+' total')
                        .set_index('col2', append=True)]).sort_index()


                        Output:



                                      col3
                        col1 col2
                        A q1 2
                        q2 4
                        A total 6
                        B q1 5
                        q2 2
                        B total 7





                        share|improve this answer
























                          2












                          2








                          2






                          You can try this also:



                          d = df.groupby(['col1','col2']).sum()
                          pd.concat([d,
                          d.sum(level=0).assign(col2='')
                          .rename(index=lambda x: x+' total')
                          .set_index('col2', append=True)]).sort_index()


                          Output:



                                        col3
                          col1 col2
                          A q1 2
                          q2 4
                          A total 6
                          B q1 5
                          q2 2
                          B total 7





                          share|improve this answer












                          You can try this also:



                          d = df.groupby(['col1','col2']).sum()
                          pd.concat([d,
                          d.sum(level=0).assign(col2='')
                          .rename(index=lambda x: x+' total')
                          .set_index('col2', append=True)]).sort_index()


                          Output:



                                        col3
                          col1 col2
                          A q1 2
                          q2 4
                          A total 6
                          B q1 5
                          q2 2
                          B total 7






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 27 '18 at 16:15









                          Scott Boston

                          51.3k72955




                          51.3k72955






























                              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%2f53947377%2fadding-a-grand-total-when-using-the-groupedby-function-on-a-dataframe%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

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas