How to compare 3 columns of DataFrame together, Python 3.6












1















I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"



Below my code:



L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']

DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})


I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
I need script like below:



DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']









share|improve this question





























    1















    I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"



    Below my code:



    L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
    L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
    L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']

    DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})


    I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
    I need script like below:



    DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']









    share|improve this question



























      1












      1








      1








      I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"



      Below my code:



      L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
      L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
      L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']

      DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})


      I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
      I need script like below:



      DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']









      share|improve this question
















      I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"



      Below my code:



      L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
      L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
      L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']

      DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})


      I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
      I need script like below:



      DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']






      python python-3.x pandas list dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:10







      SPy

















      asked Nov 13 '18 at 14:06









      SPySPy

      497519




      497519
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:



          DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) & 
          (DF1['dimId'] == DF1['sqlTableColumn'])


          General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:



          cols = ['dimId','dimName','sqlTableColumn']
          DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
          print (DF1)
          dimId dimName sqlTableColumn Id_Name_Table_Matching
          0 Region Region Region True
          1 Col2 Col2 Col2 True
          2 Col3 Col3 Col3 True
          3 Col4 Col4 Col4 True
          4 Col5 Col5 Col5 True


          Detail:



          print (DF1[cols].eq(DF1[cols[0]], axis=0))
          dimId dimName sqlTableColumn
          0 True True True
          1 True True True
          2 True True True
          3 True True True
          4 True True True





          share|improve this answer





















          • 1





            thanks, First one ok - easy one

            – SPy
            Nov 13 '18 at 14:20



















          0














          Look if this helps. Using .apply()



          df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
          print(df)


          Output:



              dimId dimName sqlTableColumn  Id_Name_Table_Matching
          0 Region Region Region True
          1 Col2 Col2 Col2 True
          2 Col3 Col3 Col3 True
          3 Col4 Col4 Col4 True
          4 Col5 Col5 Col5 True





          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%2f53282823%2fhow-to-compare-3-columns-of-dataframe-together-python-3-6%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









            1














            Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:



            DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) & 
            (DF1['dimId'] == DF1['sqlTableColumn'])


            General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:



            cols = ['dimId','dimName','sqlTableColumn']
            DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
            print (DF1)
            dimId dimName sqlTableColumn Id_Name_Table_Matching
            0 Region Region Region True
            1 Col2 Col2 Col2 True
            2 Col3 Col3 Col3 True
            3 Col4 Col4 Col4 True
            4 Col5 Col5 Col5 True


            Detail:



            print (DF1[cols].eq(DF1[cols[0]], axis=0))
            dimId dimName sqlTableColumn
            0 True True True
            1 True True True
            2 True True True
            3 True True True
            4 True True True





            share|improve this answer





















            • 1





              thanks, First one ok - easy one

              – SPy
              Nov 13 '18 at 14:20
















            1














            Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:



            DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) & 
            (DF1['dimId'] == DF1['sqlTableColumn'])


            General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:



            cols = ['dimId','dimName','sqlTableColumn']
            DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
            print (DF1)
            dimId dimName sqlTableColumn Id_Name_Table_Matching
            0 Region Region Region True
            1 Col2 Col2 Col2 True
            2 Col3 Col3 Col3 True
            3 Col4 Col4 Col4 True
            4 Col5 Col5 Col5 True


            Detail:



            print (DF1[cols].eq(DF1[cols[0]], axis=0))
            dimId dimName sqlTableColumn
            0 True True True
            1 True True True
            2 True True True
            3 True True True
            4 True True True





            share|improve this answer





















            • 1





              thanks, First one ok - easy one

              – SPy
              Nov 13 '18 at 14:20














            1












            1








            1







            Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:



            DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) & 
            (DF1['dimId'] == DF1['sqlTableColumn'])


            General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:



            cols = ['dimId','dimName','sqlTableColumn']
            DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
            print (DF1)
            dimId dimName sqlTableColumn Id_Name_Table_Matching
            0 Region Region Region True
            1 Col2 Col2 Col2 True
            2 Col3 Col3 Col3 True
            3 Col4 Col4 Col4 True
            4 Col5 Col5 Col5 True


            Detail:



            print (DF1[cols].eq(DF1[cols[0]], axis=0))
            dimId dimName sqlTableColumn
            0 True True True
            1 True True True
            2 True True True
            3 True True True
            4 True True True





            share|improve this answer















            Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:



            DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) & 
            (DF1['dimId'] == DF1['sqlTableColumn'])


            General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:



            cols = ['dimId','dimName','sqlTableColumn']
            DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
            print (DF1)
            dimId dimName sqlTableColumn Id_Name_Table_Matching
            0 Region Region Region True
            1 Col2 Col2 Col2 True
            2 Col3 Col3 Col3 True
            3 Col4 Col4 Col4 True
            4 Col5 Col5 Col5 True


            Detail:



            print (DF1[cols].eq(DF1[cols[0]], axis=0))
            dimId dimName sqlTableColumn
            0 True True True
            1 True True True
            2 True True True
            3 True True True
            4 True True True






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 14:17

























            answered Nov 13 '18 at 14:12









            jezraeljezrael

            329k23270349




            329k23270349








            • 1





              thanks, First one ok - easy one

              – SPy
              Nov 13 '18 at 14:20














            • 1





              thanks, First one ok - easy one

              – SPy
              Nov 13 '18 at 14:20








            1




            1





            thanks, First one ok - easy one

            – SPy
            Nov 13 '18 at 14:20





            thanks, First one ok - easy one

            – SPy
            Nov 13 '18 at 14:20













            0














            Look if this helps. Using .apply()



            df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
            print(df)


            Output:



                dimId dimName sqlTableColumn  Id_Name_Table_Matching
            0 Region Region Region True
            1 Col2 Col2 Col2 True
            2 Col3 Col3 Col3 True
            3 Col4 Col4 Col4 True
            4 Col5 Col5 Col5 True





            share|improve this answer






























              0














              Look if this helps. Using .apply()



              df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
              print(df)


              Output:



                  dimId dimName sqlTableColumn  Id_Name_Table_Matching
              0 Region Region Region True
              1 Col2 Col2 Col2 True
              2 Col3 Col3 Col3 True
              3 Col4 Col4 Col4 True
              4 Col5 Col5 Col5 True





              share|improve this answer




























                0












                0








                0







                Look if this helps. Using .apply()



                df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
                print(df)


                Output:



                    dimId dimName sqlTableColumn  Id_Name_Table_Matching
                0 Region Region Region True
                1 Col2 Col2 Col2 True
                2 Col3 Col3 Col3 True
                3 Col4 Col4 Col4 True
                4 Col5 Col5 Col5 True





                share|improve this answer















                Look if this helps. Using .apply()



                df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
                print(df)


                Output:



                    dimId dimName sqlTableColumn  Id_Name_Table_Matching
                0 Region Region Region True
                1 Col2 Col2 Col2 True
                2 Col3 Col3 Col3 True
                3 Col4 Col4 Col4 True
                4 Col5 Col5 Col5 True






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 29 '18 at 18:11

























                answered Nov 13 '18 at 14:38









                Srce CdeSrce Cde

                1,144511




                1,144511






























                    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%2f53282823%2fhow-to-compare-3-columns-of-dataframe-together-python-3-6%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

                    The Sandy Post

                    Danny Elfman

                    Pages that link to "Head v. Amoskeag Manufacturing Co."