To merge multiple columns into one column and count the repetition of unique values and maintain a separate column for each count in pandas dataframe2019 Community Moderator ElectionSelecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeDelete column from pandas DataFrame by column nameSet value for particular cell in pandas DataFrame using index“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersHow to count the NaN values in a column in pandas DataFrame

Silly Sally's Movie

Coworker uses her breast-pump everywhere in the office

When two POV characters meet

Why doesn't the EU now just force the UK to choose between referendum and no-deal?

Potentiometer like component

validation vs test vs training accuracy, which one to compare for claiming overfit?

It's a yearly task, alright

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Can someone explain this Mudra, done by Ramakrishna Paramhansa in Samadhi?

Have researches managed to "reverse time" and if so, what does that mean for physics?

"One can do his homework in the library"

What is the difference between "shut" and "close"?

Question about partial fractions with irreducible quadratic factors

Counter-example to the existence of left Bousfield localization of combinatorial model category

The three point beverage

Does the Bracer of Flying Daggers benefit from the Dueling Fighting style?

Who is our nearest neighbor

Prove that the total distance is minimised (when travelling across the longest path)

Why do Australian milk farmers need to protest supermarkets' milk price?

Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?

Can infringement of a trademark be pursued for using a company's name in a sentence?

How do anti-virus programs start at Windows boot?

Should QA ask requirements to developers?



To merge multiple columns into one column and count the repetition of unique values and maintain a separate column for each count in pandas dataframe



2019 Community Moderator ElectionSelecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeDelete column from pandas DataFrame by column nameSet value for particular cell in pandas DataFrame using index“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersHow to count the NaN values in a column in pandas DataFrame










3















I have a Pandas data frame like this:



 Q1 Q2 Q3 Q4 
0 Bachelor Postgrad Postgrad Masters
1 Bachelor Postgrad Postgrad Bachelor
2 Masters Postgrad Postgrad Masters
3 Bachelor Bachelor Bachelor Masters
4 Bachelor NaN NaN Masters Masters
...


I want to add columns like this:



 Q1 Q2 Q3 Q4 Bachelor Masters Postgrad 
0 Bachelor Postgrad Postgrad Masters 1 1 2
1 Bachelor Postgrad Postgrad Bachelor 2 0 2
2 Masters Postgrad Postgrad Masters 0 2 2
3 Bachelor Bachelor Bachelor Masters 3 1 0
4 Bachelor NaN Masters Masters 1 1 1
...


I tried and able to merge Q1 to Q4 into one column but unable to count the unique values and print these count in the seperate column. Any help to this will be appreciated.










share|improve this question


























    3















    I have a Pandas data frame like this:



     Q1 Q2 Q3 Q4 
    0 Bachelor Postgrad Postgrad Masters
    1 Bachelor Postgrad Postgrad Bachelor
    2 Masters Postgrad Postgrad Masters
    3 Bachelor Bachelor Bachelor Masters
    4 Bachelor NaN NaN Masters Masters
    ...


    I want to add columns like this:



     Q1 Q2 Q3 Q4 Bachelor Masters Postgrad 
    0 Bachelor Postgrad Postgrad Masters 1 1 2
    1 Bachelor Postgrad Postgrad Bachelor 2 0 2
    2 Masters Postgrad Postgrad Masters 0 2 2
    3 Bachelor Bachelor Bachelor Masters 3 1 0
    4 Bachelor NaN Masters Masters 1 1 1
    ...


    I tried and able to merge Q1 to Q4 into one column but unable to count the unique values and print these count in the seperate column. Any help to this will be appreciated.










    share|improve this question
























      3












      3








      3








      I have a Pandas data frame like this:



       Q1 Q2 Q3 Q4 
      0 Bachelor Postgrad Postgrad Masters
      1 Bachelor Postgrad Postgrad Bachelor
      2 Masters Postgrad Postgrad Masters
      3 Bachelor Bachelor Bachelor Masters
      4 Bachelor NaN NaN Masters Masters
      ...


      I want to add columns like this:



       Q1 Q2 Q3 Q4 Bachelor Masters Postgrad 
      0 Bachelor Postgrad Postgrad Masters 1 1 2
      1 Bachelor Postgrad Postgrad Bachelor 2 0 2
      2 Masters Postgrad Postgrad Masters 0 2 2
      3 Bachelor Bachelor Bachelor Masters 3 1 0
      4 Bachelor NaN Masters Masters 1 1 1
      ...


      I tried and able to merge Q1 to Q4 into one column but unable to count the unique values and print these count in the seperate column. Any help to this will be appreciated.










      share|improve this question














      I have a Pandas data frame like this:



       Q1 Q2 Q3 Q4 
      0 Bachelor Postgrad Postgrad Masters
      1 Bachelor Postgrad Postgrad Bachelor
      2 Masters Postgrad Postgrad Masters
      3 Bachelor Bachelor Bachelor Masters
      4 Bachelor NaN NaN Masters Masters
      ...


      I want to add columns like this:



       Q1 Q2 Q3 Q4 Bachelor Masters Postgrad 
      0 Bachelor Postgrad Postgrad Masters 1 1 2
      1 Bachelor Postgrad Postgrad Bachelor 2 0 2
      2 Masters Postgrad Postgrad Masters 0 2 2
      3 Bachelor Bachelor Bachelor Masters 3 1 0
      4 Bachelor NaN Masters Masters 1 1 1
      ...


      I tried and able to merge Q1 to Q4 into one column but unable to count the unique values and print these count in the seperate column. Any help to this will be appreciated.







      python-3.x pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 6 at 17:41









      vishal_agvishal_ag

      203




      203






















          1 Answer
          1






          active

          oldest

          votes


















          2














          You are looking for get_dummies



          s=pd.get_dummies(df,prefix='', prefix_sep='').sum(1,level=0)
          s
          Out[502]:
          Bachelor Masters Postgrad
          0 1 1 2
          1 2 0 2
          2 0 2 2
          3 3 1 0
          4 1 2 0
          # then using concat
          df=pd.concat([df,s],axis=1)





          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%2f55029189%2fto-merge-multiple-columns-into-one-column-and-count-the-repetition-of-unique-val%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









            2














            You are looking for get_dummies



            s=pd.get_dummies(df,prefix='', prefix_sep='').sum(1,level=0)
            s
            Out[502]:
            Bachelor Masters Postgrad
            0 1 1 2
            1 2 0 2
            2 0 2 2
            3 3 1 0
            4 1 2 0
            # then using concat
            df=pd.concat([df,s],axis=1)





            share|improve this answer



























              2














              You are looking for get_dummies



              s=pd.get_dummies(df,prefix='', prefix_sep='').sum(1,level=0)
              s
              Out[502]:
              Bachelor Masters Postgrad
              0 1 1 2
              1 2 0 2
              2 0 2 2
              3 3 1 0
              4 1 2 0
              # then using concat
              df=pd.concat([df,s],axis=1)





              share|improve this answer

























                2












                2








                2







                You are looking for get_dummies



                s=pd.get_dummies(df,prefix='', prefix_sep='').sum(1,level=0)
                s
                Out[502]:
                Bachelor Masters Postgrad
                0 1 1 2
                1 2 0 2
                2 0 2 2
                3 3 1 0
                4 1 2 0
                # then using concat
                df=pd.concat([df,s],axis=1)





                share|improve this answer













                You are looking for get_dummies



                s=pd.get_dummies(df,prefix='', prefix_sep='').sum(1,level=0)
                s
                Out[502]:
                Bachelor Masters Postgrad
                0 1 1 2
                1 2 0 2
                2 0 2 2
                3 3 1 0
                4 1 2 0
                # then using concat
                df=pd.concat([df,s],axis=1)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 6 at 17:45









                Wen-BenWen-Ben

                117k83369




                117k83369





























                    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%2f55029189%2fto-merge-multiple-columns-into-one-column-and-count-the-repetition-of-unique-val%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

                    AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                    Алба-Юлія

                    Захаров Федір Захарович