np.logical_and with three tests










1















I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).



Some sample code:



df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test.head()


When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
I receive the error TypeError: return arrays must be of ArrayType So I try the following:



df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
np.logical_and(df_test['c'] == 0,
df_test['d'] == 0)),
True, False).astype(int)
df_test.head()


And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.



 a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 1
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1


I need a way to evaluate 3 tests and return true or false as an int.










share|improve this question


























    1















    I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).



    Some sample code:



    df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
    (None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
    ('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
    df_test.head()


    When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
    I receive the error TypeError: return arrays must be of ArrayType So I try the following:



    df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
    (None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
    ('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
    df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
    np.logical_and(df_test['c'] == 0,
    df_test['d'] == 0)),
    True, False).astype(int)
    df_test.head()


    And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.



     a b c d e f g
    0 ? 2.0 1 0 0 0 0
    1 None 2.0 1 0 0 0 0
    2 None 2.0 0 0 0 0 1
    3 None 2.0 0 1 0 0 0
    4 ? 2.0 0 0 0 0 1


    I need a way to evaluate 3 tests and return true or false as an int.










    share|improve this question
























      1












      1








      1








      I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).



      Some sample code:



      df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
      (None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
      ('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
      df_test.head()


      When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
      I receive the error TypeError: return arrays must be of ArrayType So I try the following:



      df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
      (None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
      ('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
      df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
      np.logical_and(df_test['c'] == 0,
      df_test['d'] == 0)),
      True, False).astype(int)
      df_test.head()


      And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.



       a b c d e f g
      0 ? 2.0 1 0 0 0 0
      1 None 2.0 1 0 0 0 0
      2 None 2.0 0 0 0 0 1
      3 None 2.0 0 1 0 0 0
      4 ? 2.0 0 0 0 0 1


      I need a way to evaluate 3 tests and return true or false as an int.










      share|improve this question














      I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).



      Some sample code:



      df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
      (None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
      ('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
      df_test.head()


      When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
      I receive the error TypeError: return arrays must be of ArrayType So I try the following:



      df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0), 
      (None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
      ('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
      df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
      np.logical_and(df_test['c'] == 0,
      df_test['d'] == 0)),
      True, False).astype(int)
      df_test.head()


      And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.



       a b c d e f g
      0 ? 2.0 1 0 0 0 0
      1 None 2.0 1 0 0 0 0
      2 None 2.0 0 0 0 0 1
      3 None 2.0 0 1 0 0 0
      4 ? 2.0 0 0 0 0 1


      I need a way to evaluate 3 tests and return true or false as an int.







      python pandas numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 16:59









      EricEric

      1471214




      1471214






















          2 Answers
          2






          active

          oldest

          votes


















          3














          For 3 or more conditions, use np.logical_and.reduce and pass a list of masks;



          mask = np.logical_and.reduce([
          df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
          df_test['g'] = mask.astype(int)

          print(df_test)
          a b c d e f g
          0 ? 2.0 1 0 0 0 0
          1 None 2.0 1 0 0 0 0
          2 None 2.0 0 0 0 0 0
          3 None 2.0 0 1 0 0 0
          4 ? 2.0 0 0 0 0 1


          The subsequent np.where is superfluous here.






          share|improve this answer






























            1














            Try:



            df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)



            df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)


            Output:



             a b c d e f htest
            0 ? 2.0 1 0 0 0 0
            1 None 2.0 1 0 0 0 0
            2 None 2.0 0 0 0 0 0
            3 None 2.0 0 1 0 0 0
            4 ? 2.0 0 0 0 0 1





            share|improve this answer

























            • Think the first condition should be notna.

              – coldspeed
              Nov 12 '18 at 17:14











            • yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

              – Eric
              Nov 12 '18 at 17:19











            • Ah.. Got thanks.

              – Scott Boston
              Nov 12 '18 at 17:22










            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%2f53266807%2fnp-logical-and-with-three-tests%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









            3














            For 3 or more conditions, use np.logical_and.reduce and pass a list of masks;



            mask = np.logical_and.reduce([
            df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
            df_test['g'] = mask.astype(int)

            print(df_test)
            a b c d e f g
            0 ? 2.0 1 0 0 0 0
            1 None 2.0 1 0 0 0 0
            2 None 2.0 0 0 0 0 0
            3 None 2.0 0 1 0 0 0
            4 ? 2.0 0 0 0 0 1


            The subsequent np.where is superfluous here.






            share|improve this answer



























              3














              For 3 or more conditions, use np.logical_and.reduce and pass a list of masks;



              mask = np.logical_and.reduce([
              df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
              df_test['g'] = mask.astype(int)

              print(df_test)
              a b c d e f g
              0 ? 2.0 1 0 0 0 0
              1 None 2.0 1 0 0 0 0
              2 None 2.0 0 0 0 0 0
              3 None 2.0 0 1 0 0 0
              4 ? 2.0 0 0 0 0 1


              The subsequent np.where is superfluous here.






              share|improve this answer

























                3












                3








                3







                For 3 or more conditions, use np.logical_and.reduce and pass a list of masks;



                mask = np.logical_and.reduce([
                df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
                df_test['g'] = mask.astype(int)

                print(df_test)
                a b c d e f g
                0 ? 2.0 1 0 0 0 0
                1 None 2.0 1 0 0 0 0
                2 None 2.0 0 0 0 0 0
                3 None 2.0 0 1 0 0 0
                4 ? 2.0 0 0 0 0 1


                The subsequent np.where is superfluous here.






                share|improve this answer













                For 3 or more conditions, use np.logical_and.reduce and pass a list of masks;



                mask = np.logical_and.reduce([
                df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
                df_test['g'] = mask.astype(int)

                print(df_test)
                a b c d e f g
                0 ? 2.0 1 0 0 0 0
                1 None 2.0 1 0 0 0 0
                2 None 2.0 0 0 0 0 0
                3 None 2.0 0 1 0 0 0
                4 ? 2.0 0 0 0 0 1


                The subsequent np.where is superfluous here.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 17:10









                coldspeedcoldspeed

                126k23126213




                126k23126213























                    1














                    Try:



                    df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)



                    df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)


                    Output:



                     a b c d e f htest
                    0 ? 2.0 1 0 0 0 0
                    1 None 2.0 1 0 0 0 0
                    2 None 2.0 0 0 0 0 0
                    3 None 2.0 0 1 0 0 0
                    4 ? 2.0 0 0 0 0 1





                    share|improve this answer

























                    • Think the first condition should be notna.

                      – coldspeed
                      Nov 12 '18 at 17:14











                    • yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

                      – Eric
                      Nov 12 '18 at 17:19











                    • Ah.. Got thanks.

                      – Scott Boston
                      Nov 12 '18 at 17:22















                    1














                    Try:



                    df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)



                    df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)


                    Output:



                     a b c d e f htest
                    0 ? 2.0 1 0 0 0 0
                    1 None 2.0 1 0 0 0 0
                    2 None 2.0 0 0 0 0 0
                    3 None 2.0 0 1 0 0 0
                    4 ? 2.0 0 0 0 0 1





                    share|improve this answer

























                    • Think the first condition should be notna.

                      – coldspeed
                      Nov 12 '18 at 17:14











                    • yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

                      – Eric
                      Nov 12 '18 at 17:19











                    • Ah.. Got thanks.

                      – Scott Boston
                      Nov 12 '18 at 17:22













                    1












                    1








                    1







                    Try:



                    df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)



                    df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)


                    Output:



                     a b c d e f htest
                    0 ? 2.0 1 0 0 0 0
                    1 None 2.0 1 0 0 0 0
                    2 None 2.0 0 0 0 0 0
                    3 None 2.0 0 1 0 0 0
                    4 ? 2.0 0 0 0 0 1





                    share|improve this answer















                    Try:



                    df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)



                    df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)


                    Output:



                     a b c d e f htest
                    0 ? 2.0 1 0 0 0 0
                    1 None 2.0 1 0 0 0 0
                    2 None 2.0 0 0 0 0 0
                    3 None 2.0 0 1 0 0 0
                    4 ? 2.0 0 0 0 0 1






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 '18 at 17:21

























                    answered Nov 12 '18 at 17:11









                    Scott BostonScott Boston

                    53.1k72955




                    53.1k72955












                    • Think the first condition should be notna.

                      – coldspeed
                      Nov 12 '18 at 17:14











                    • yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

                      – Eric
                      Nov 12 '18 at 17:19











                    • Ah.. Got thanks.

                      – Scott Boston
                      Nov 12 '18 at 17:22

















                    • Think the first condition should be notna.

                      – coldspeed
                      Nov 12 '18 at 17:14











                    • yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

                      – Eric
                      Nov 12 '18 at 17:19











                    • Ah.. Got thanks.

                      – Scott Boston
                      Nov 12 '18 at 17:22
















                    Think the first condition should be notna.

                    – coldspeed
                    Nov 12 '18 at 17:14





                    Think the first condition should be notna.

                    – coldspeed
                    Nov 12 '18 at 17:14













                    yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

                    – Eric
                    Nov 12 '18 at 17:19





                    yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.

                    – Eric
                    Nov 12 '18 at 17:19













                    Ah.. Got thanks.

                    – Scott Boston
                    Nov 12 '18 at 17:22





                    Ah.. Got thanks.

                    – Scott Boston
                    Nov 12 '18 at 17:22

















                    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%2f53266807%2fnp-logical-and-with-three-tests%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo