Groupby Rows and Sum












0














I have the following dataframe:



print(inventory_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 0 8 0
10/09/18 5 0 2

11/09/18 4 0 0
11/09/18 0 10 0

...

And I would like to get:

print(final_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 5 8 2
11/09/18 4 10 0

...


I tried with:



final_df = inventory_df.drop_duplicates(subset=None, keep='first', inplace=False)


But it does not produce the desired output. How can I create final_df?










share|improve this question






















  • do you want to drop all zeros from your dataframe even that they are assigned to different entries?
    – CIsForCookies
    Nov 20 '18 at 10:43










  • Possible duplicate of Pandas group-by and sum
    – jpp
    Nov 20 '18 at 15:28
















0














I have the following dataframe:



print(inventory_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 0 8 0
10/09/18 5 0 2

11/09/18 4 0 0
11/09/18 0 10 0

...

And I would like to get:

print(final_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 5 8 2
11/09/18 4 10 0

...


I tried with:



final_df = inventory_df.drop_duplicates(subset=None, keep='first', inplace=False)


But it does not produce the desired output. How can I create final_df?










share|improve this question






















  • do you want to drop all zeros from your dataframe even that they are assigned to different entries?
    – CIsForCookies
    Nov 20 '18 at 10:43










  • Possible duplicate of Pandas group-by and sum
    – jpp
    Nov 20 '18 at 15:28














0












0








0







I have the following dataframe:



print(inventory_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 0 8 0
10/09/18 5 0 2

11/09/18 4 0 0
11/09/18 0 10 0

...

And I would like to get:

print(final_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 5 8 2
11/09/18 4 10 0

...


I tried with:



final_df = inventory_df.drop_duplicates(subset=None, keep='first', inplace=False)


But it does not produce the desired output. How can I create final_df?










share|improve this question













I have the following dataframe:



print(inventory_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 0 8 0
10/09/18 5 0 2

11/09/18 4 0 0
11/09/18 0 10 0

...

And I would like to get:

print(final_df)

dt_op Prod_1 Prod_2 ... Prod_n
10/09/18 5 8 2
11/09/18 4 10 0

...


I tried with:



final_df = inventory_df.drop_duplicates(subset=None, keep='first', inplace=False)


But it does not produce the desired output. How can I create final_df?







python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 10:39









Alessandro Ceccarelli

247211




247211












  • do you want to drop all zeros from your dataframe even that they are assigned to different entries?
    – CIsForCookies
    Nov 20 '18 at 10:43










  • Possible duplicate of Pandas group-by and sum
    – jpp
    Nov 20 '18 at 15:28


















  • do you want to drop all zeros from your dataframe even that they are assigned to different entries?
    – CIsForCookies
    Nov 20 '18 at 10:43










  • Possible duplicate of Pandas group-by and sum
    – jpp
    Nov 20 '18 at 15:28
















do you want to drop all zeros from your dataframe even that they are assigned to different entries?
– CIsForCookies
Nov 20 '18 at 10:43




do you want to drop all zeros from your dataframe even that they are assigned to different entries?
– CIsForCookies
Nov 20 '18 at 10:43












Possible duplicate of Pandas group-by and sum
– jpp
Nov 20 '18 at 15:28




Possible duplicate of Pandas group-by and sum
– jpp
Nov 20 '18 at 15:28












2 Answers
2






active

oldest

votes


















0














Just simulated the Stated DataFrame, you asked about the groupby + sum() across the rows.



Reproduced DataFrame:



>>> df
dt_op Prod_1 Prod_2 Prod_n
0 10/09/18 0 8 0
1 10/09/18 5 0 2
2 11/09/18 4 0 0


Using groupby around the columns axis=1(of dimension 1, which is what used to be columns) or simply df.groupby('dt_op').sum :



>>> df.groupby('dt_op').sum(axis=1)
Prod_1 Prod_2 Prod_n
dt_op
10/09/18 5 8 2
11/09/18 4 0 0


However, you are looking for the literal sum() of rows across the columns:



>>> df['new_sum'] = df.sum(axis=1)
>>> df
dt_op Prod_1 Prod_2 Prod_n new_sum
0 10/09/18 0 8 0 8
1 10/09/18 5 0 2 7
2 11/09/18 4 0 0 4





share|improve this answer































    1














    You can use pandas groupby function with sum():



    In [412]: inventory_df
    Out[412]:
    dt_op Prod_1 Prod_2
    0 10/09/18 0 8
    1 10/09/18 5 0
    2 11/09/18 4 0
    3 11/09/18 0 10

    In [413]: inventory_df.groupby('dt_op').sum()
    Out[413]:
    Prod_1 Prod_2
    dt_op
    10/09/18 5 8
    11/09/18 4 10





    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%2f53391183%2fgroupby-rows-and-sum%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









      0














      Just simulated the Stated DataFrame, you asked about the groupby + sum() across the rows.



      Reproduced DataFrame:



      >>> df
      dt_op Prod_1 Prod_2 Prod_n
      0 10/09/18 0 8 0
      1 10/09/18 5 0 2
      2 11/09/18 4 0 0


      Using groupby around the columns axis=1(of dimension 1, which is what used to be columns) or simply df.groupby('dt_op').sum :



      >>> df.groupby('dt_op').sum(axis=1)
      Prod_1 Prod_2 Prod_n
      dt_op
      10/09/18 5 8 2
      11/09/18 4 0 0


      However, you are looking for the literal sum() of rows across the columns:



      >>> df['new_sum'] = df.sum(axis=1)
      >>> df
      dt_op Prod_1 Prod_2 Prod_n new_sum
      0 10/09/18 0 8 0 8
      1 10/09/18 5 0 2 7
      2 11/09/18 4 0 0 4





      share|improve this answer




























        0














        Just simulated the Stated DataFrame, you asked about the groupby + sum() across the rows.



        Reproduced DataFrame:



        >>> df
        dt_op Prod_1 Prod_2 Prod_n
        0 10/09/18 0 8 0
        1 10/09/18 5 0 2
        2 11/09/18 4 0 0


        Using groupby around the columns axis=1(of dimension 1, which is what used to be columns) or simply df.groupby('dt_op').sum :



        >>> df.groupby('dt_op').sum(axis=1)
        Prod_1 Prod_2 Prod_n
        dt_op
        10/09/18 5 8 2
        11/09/18 4 0 0


        However, you are looking for the literal sum() of rows across the columns:



        >>> df['new_sum'] = df.sum(axis=1)
        >>> df
        dt_op Prod_1 Prod_2 Prod_n new_sum
        0 10/09/18 0 8 0 8
        1 10/09/18 5 0 2 7
        2 11/09/18 4 0 0 4





        share|improve this answer


























          0












          0








          0






          Just simulated the Stated DataFrame, you asked about the groupby + sum() across the rows.



          Reproduced DataFrame:



          >>> df
          dt_op Prod_1 Prod_2 Prod_n
          0 10/09/18 0 8 0
          1 10/09/18 5 0 2
          2 11/09/18 4 0 0


          Using groupby around the columns axis=1(of dimension 1, which is what used to be columns) or simply df.groupby('dt_op').sum :



          >>> df.groupby('dt_op').sum(axis=1)
          Prod_1 Prod_2 Prod_n
          dt_op
          10/09/18 5 8 2
          11/09/18 4 0 0


          However, you are looking for the literal sum() of rows across the columns:



          >>> df['new_sum'] = df.sum(axis=1)
          >>> df
          dt_op Prod_1 Prod_2 Prod_n new_sum
          0 10/09/18 0 8 0 8
          1 10/09/18 5 0 2 7
          2 11/09/18 4 0 0 4





          share|improve this answer














          Just simulated the Stated DataFrame, you asked about the groupby + sum() across the rows.



          Reproduced DataFrame:



          >>> df
          dt_op Prod_1 Prod_2 Prod_n
          0 10/09/18 0 8 0
          1 10/09/18 5 0 2
          2 11/09/18 4 0 0


          Using groupby around the columns axis=1(of dimension 1, which is what used to be columns) or simply df.groupby('dt_op').sum :



          >>> df.groupby('dt_op').sum(axis=1)
          Prod_1 Prod_2 Prod_n
          dt_op
          10/09/18 5 8 2
          11/09/18 4 0 0


          However, you are looking for the literal sum() of rows across the columns:



          >>> df['new_sum'] = df.sum(axis=1)
          >>> df
          dt_op Prod_1 Prod_2 Prod_n new_sum
          0 10/09/18 0 8 0 8
          1 10/09/18 5 0 2 7
          2 11/09/18 4 0 0 4






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 15:36

























          answered Nov 20 '18 at 15:21









          pygo

          2,1511617




          2,1511617

























              1














              You can use pandas groupby function with sum():



              In [412]: inventory_df
              Out[412]:
              dt_op Prod_1 Prod_2
              0 10/09/18 0 8
              1 10/09/18 5 0
              2 11/09/18 4 0
              3 11/09/18 0 10

              In [413]: inventory_df.groupby('dt_op').sum()
              Out[413]:
              Prod_1 Prod_2
              dt_op
              10/09/18 5 8
              11/09/18 4 10





              share|improve this answer




























                1














                You can use pandas groupby function with sum():



                In [412]: inventory_df
                Out[412]:
                dt_op Prod_1 Prod_2
                0 10/09/18 0 8
                1 10/09/18 5 0
                2 11/09/18 4 0
                3 11/09/18 0 10

                In [413]: inventory_df.groupby('dt_op').sum()
                Out[413]:
                Prod_1 Prod_2
                dt_op
                10/09/18 5 8
                11/09/18 4 10





                share|improve this answer


























                  1












                  1








                  1






                  You can use pandas groupby function with sum():



                  In [412]: inventory_df
                  Out[412]:
                  dt_op Prod_1 Prod_2
                  0 10/09/18 0 8
                  1 10/09/18 5 0
                  2 11/09/18 4 0
                  3 11/09/18 0 10

                  In [413]: inventory_df.groupby('dt_op').sum()
                  Out[413]:
                  Prod_1 Prod_2
                  dt_op
                  10/09/18 5 8
                  11/09/18 4 10





                  share|improve this answer














                  You can use pandas groupby function with sum():



                  In [412]: inventory_df
                  Out[412]:
                  dt_op Prod_1 Prod_2
                  0 10/09/18 0 8
                  1 10/09/18 5 0
                  2 11/09/18 4 0
                  3 11/09/18 0 10

                  In [413]: inventory_df.groupby('dt_op').sum()
                  Out[413]:
                  Prod_1 Prod_2
                  dt_op
                  10/09/18 5 8
                  11/09/18 4 10






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 12:23

























                  answered Nov 20 '18 at 10:44









                  Mayank Porwal

                  4,4991624




                  4,4991624






























                      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%2f53391183%2fgroupby-rows-and-sum%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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy