How to create a df that gets sum of columns based on a groupby column? The Next CEO of Stack Overflow2019 Community Moderator ElectionCreate a new column based on two columns from two different dataframesHow to sum values grouped by two columns in pandasCreate new data frames from existing data frame based on unique column valuesLow silhouette coefficientShould I use pandas get_dummies and create additional columns or use my own encoding code that keeps 1 column?How to calculate Cumulative Sum with Groupby in Python?How to create a new column based on two other columns in Pandas?how to update column in data frame based on conditionPandas merge column duplicate and sum valueMultiple filtering pandas columns based on values in another column

Can I hook these wires up to find the connection to a dead outlet?

Could a dragon use hot air to help it take off?

How badly should I try to prevent a user from XSSing themselves?

Prodigo = pro + ago?

My singleton can be called multiple times

Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?

How dangerous is XSS

Is it possible to create a QR code using text?

A hang glider, sudden unexpected lift to 25,000 feet altitude, what could do this?

Oldie but Goldie

How exploitable/balanced is this homebrew spell: Spell Permanency?

Read/write a pipe-delimited file line by line with some simple text manipulation

Is a distribution that is normal, but highly skewed, considered Gaussian?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

pgfplots: How to draw a tangent graph below two others?

Can you teleport closer to a creature you are Frightened of?

What does this strange code stamp on my passport mean?

How to travel Japan while expressing milk?

Was the Stack Exchange "Happy April Fools" page fitting with the 90s code?

Can this transistor (2N2222) take 6 V on emitter-base? Am I reading the datasheet incorrectly?

Is it possible to get a referendum by a court decision?

How to detect int overflow in C

Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?

Find the majority element, which appears more than half the time



How to create a df that gets sum of columns based on a groupby column?



The Next CEO of Stack Overflow
2019 Community Moderator ElectionCreate a new column based on two columns from two different dataframesHow to sum values grouped by two columns in pandasCreate new data frames from existing data frame based on unique column valuesLow silhouette coefficientShould I use pandas get_dummies and create additional columns or use my own encoding code that keeps 1 column?How to calculate Cumulative Sum with Groupby in Python?How to create a new column based on two other columns in Pandas?how to update column in data frame based on conditionPandas merge column duplicate and sum valueMultiple filtering pandas columns based on values in another column










0












$begingroup$


country year gender measure value0 ... value12
A 2000 1 vaccinated_at_month 2 ... 1
B 2000 1 vaccinated_at_month 13 ... 12
A 2000 0 vaccinated_at_month 4 ... 3
A 2000 9 vaccinated_at_month 5 ... 4
B 2000 0 walked_at_month 3 ... 13
C 2001 1 vaccinated_at_month 6 ... 5
C 2001 0 vaccinated_at_month 3 ... 2


I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.



I have tried



df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())


However, df_new still preserves the gender column. I am trying to get this outcome:



country year measure value0 ... value12
A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
B 2000 vaccinated_at_month 13 ... 12
B 2000 walked_at_month 3 ... 13
C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
C 2001 vaccinated_at_month 3 ... 2









share|improve this question











$endgroup$
















    0












    $begingroup$


    country year gender measure value0 ... value12
    A 2000 1 vaccinated_at_month 2 ... 1
    B 2000 1 vaccinated_at_month 13 ... 12
    A 2000 0 vaccinated_at_month 4 ... 3
    A 2000 9 vaccinated_at_month 5 ... 4
    B 2000 0 walked_at_month 3 ... 13
    C 2001 1 vaccinated_at_month 6 ... 5
    C 2001 0 vaccinated_at_month 3 ... 2


    I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.



    I have tried



    df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())


    However, df_new still preserves the gender column. I am trying to get this outcome:



    country year measure value0 ... value12
    A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
    B 2000 vaccinated_at_month 13 ... 12
    B 2000 walked_at_month 3 ... 13
    C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
    C 2001 vaccinated_at_month 3 ... 2









    share|improve this question











    $endgroup$














      0












      0








      0





      $begingroup$


      country year gender measure value0 ... value12
      A 2000 1 vaccinated_at_month 2 ... 1
      B 2000 1 vaccinated_at_month 13 ... 12
      A 2000 0 vaccinated_at_month 4 ... 3
      A 2000 9 vaccinated_at_month 5 ... 4
      B 2000 0 walked_at_month 3 ... 13
      C 2001 1 vaccinated_at_month 6 ... 5
      C 2001 0 vaccinated_at_month 3 ... 2


      I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.



      I have tried



      df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())


      However, df_new still preserves the gender column. I am trying to get this outcome:



      country year measure value0 ... value12
      A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
      B 2000 vaccinated_at_month 13 ... 12
      B 2000 walked_at_month 3 ... 13
      C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
      C 2001 vaccinated_at_month 3 ... 2









      share|improve this question











      $endgroup$




      country year gender measure value0 ... value12
      A 2000 1 vaccinated_at_month 2 ... 1
      B 2000 1 vaccinated_at_month 13 ... 12
      A 2000 0 vaccinated_at_month 4 ... 3
      A 2000 9 vaccinated_at_month 5 ... 4
      B 2000 0 walked_at_month 3 ... 13
      C 2001 1 vaccinated_at_month 6 ... 5
      C 2001 0 vaccinated_at_month 3 ... 2


      I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.



      I have tried



      df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())


      However, df_new still preserves the gender column. I am trying to get this outcome:



      country year measure value0 ... value12
      A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
      B 2000 vaccinated_at_month 13 ... 12
      B 2000 walked_at_month 3 ... 13
      C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
      C 2001 vaccinated_at_month 3 ... 2






      python pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 18:26









      Simon Larsson

      588112




      588112










      asked Mar 25 at 15:32









      user70182user70182

      41




      41




















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          This question is more appropriate at Stackoverflow since it is more of a programming question.



          However, I think you almost got it working. Just remove gender from your groupby:



          df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())





          share|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "557"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fdatascience.stackexchange.com%2fquestions%2f47953%2fhow-to-create-a-df-that-gets-sum-of-columns-based-on-a-groupby-column%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












            $begingroup$

            This question is more appropriate at Stackoverflow since it is more of a programming question.



            However, I think you almost got it working. Just remove gender from your groupby:



            df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())





            share|improve this answer









            $endgroup$

















              0












              $begingroup$

              This question is more appropriate at Stackoverflow since it is more of a programming question.



              However, I think you almost got it working. Just remove gender from your groupby:



              df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())





              share|improve this answer









              $endgroup$















                0












                0








                0





                $begingroup$

                This question is more appropriate at Stackoverflow since it is more of a programming question.



                However, I think you almost got it working. Just remove gender from your groupby:



                df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())





                share|improve this answer









                $endgroup$



                This question is more appropriate at Stackoverflow since it is more of a programming question.



                However, I think you almost got it working. Just remove gender from your groupby:



                df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 16:46









                Simon LarssonSimon Larsson

                588112




                588112



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Data Science Stack Exchange!


                    • 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.

                    Use MathJax to format equations. MathJax reference.


                    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%2fdatascience.stackexchange.com%2fquestions%2f47953%2fhow-to-create-a-df-that-gets-sum-of-columns-based-on-a-groupby-column%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

                    Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                    Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

                    Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High