Python Pandas: Boolean indexing on multiple columns [duplicate]selecting across multiple columns with python pandas?Filtering Panda DataFrameReplace row in DataFrame dependant on a valueFill NaN with a known valueChange entire pandas Series based on conditionsCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDoes Python have a ternary conditional operator?Accessing the index in 'for' loops?Does Python have a string 'contains' substring method?Selecting multiple columns in a pandas dataframeRenaming columns in pandasDelete column from pandas DataFrame by column nameSelect rows from a DataFrame based on values in a column in pandas

Font hinting is lost in Chrome-like browsers (for some languages )

tikz: show 0 at the axis origin

The use of multiple foreign keys on same column in SQL Server

Why was the small council so happy for Tyrion to become the Master of Coin?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

Is this a crack on the carbon frame?

How do I create uniquely male characters?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

What defenses are there against being summoned by the Gate spell?

How to format long polynomial?

Prove that NP is closed under karp reduction?

What is the word for reserving something for yourself before others do?

What are the differences between the usage of 'it' and 'they'?

strToHex ( string to its hex representation as string)

Can a Warlock become Neutral Good?

How old can references or sources in a thesis be?

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Which models of the Boeing 737 are still in production?

Why, historically, did Gödel think CH was false?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Writing rule stating superpower from different root cause is bad writing



Python Pandas: Boolean indexing on multiple columns [duplicate]


selecting across multiple columns with python pandas?Filtering Panda DataFrameReplace row in DataFrame dependant on a valueFill NaN with a known valueChange entire pandas Series based on conditionsCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDoes Python have a ternary conditional operator?Accessing the index in 'for' loops?Does Python have a string 'contains' substring method?Selecting multiple columns in a pandas dataframeRenaming columns in pandasDelete column from pandas DataFrame by column nameSelect rows from a DataFrame based on values in a column in pandas






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








26
















This question already has an answer here:



  • selecting across multiple columns with python pandas?

    3 answers



despite there being at least two good tutorials on how to index a DataFrame in Python's pandas library, I still can't work out an elegant way of SELECTing on more than one column.



>>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


I have found (what I think is) a rather inelegant way of doing it, like this



>>> d[d['x']>2][d['y']>7]


But it's not pretty, and it scores fairly low for readability (I think).



Is there a better, more Python-tastic way?










share|improve this question













marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























    26
















    This question already has an answer here:



    • selecting across multiple columns with python pandas?

      3 answers



    despite there being at least two good tutorials on how to index a DataFrame in Python's pandas library, I still can't work out an elegant way of SELECTing on more than one column.



    >>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
    >>> d
    x y
    0 1 4
    1 2 5
    2 3 6
    3 4 7
    4 5 8
    >>> d[d['x']>2] # This works fine
    x y
    2 3 6
    3 4 7
    4 5 8
    >>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


    I have found (what I think is) a rather inelegant way of doing it, like this



    >>> d[d['x']>2][d['y']>7]


    But it's not pretty, and it scores fairly low for readability (I think).



    Is there a better, more Python-tastic way?










    share|improve this question













    marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      26












      26








      26


      9







      This question already has an answer here:



      • selecting across multiple columns with python pandas?

        3 answers



      despite there being at least two good tutorials on how to index a DataFrame in Python's pandas library, I still can't work out an elegant way of SELECTing on more than one column.



      >>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
      >>> d
      x y
      0 1 4
      1 2 5
      2 3 6
      3 4 7
      4 5 8
      >>> d[d['x']>2] # This works fine
      x y
      2 3 6
      3 4 7
      4 5 8
      >>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


      I have found (what I think is) a rather inelegant way of doing it, like this



      >>> d[d['x']>2][d['y']>7]


      But it's not pretty, and it scores fairly low for readability (I think).



      Is there a better, more Python-tastic way?










      share|improve this question















      This question already has an answer here:



      • selecting across multiple columns with python pandas?

        3 answers



      despite there being at least two good tutorials on how to index a DataFrame in Python's pandas library, I still can't work out an elegant way of SELECTing on more than one column.



      >>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
      >>> d
      x y
      0 1 4
      1 2 5
      2 3 6
      3 4 7
      4 5 8
      >>> d[d['x']>2] # This works fine
      x y
      2 3 6
      3 4 7
      4 5 8
      >>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


      I have found (what I think is) a rather inelegant way of doing it, like this



      >>> d[d['x']>2][d['y']>7]


      But it's not pretty, and it scores fairly low for readability (I think).



      Is there a better, more Python-tastic way?





      This question already has an answer here:



      • selecting across multiple columns with python pandas?

        3 answers







      python pandas dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 20 '13 at 14:21









      LondonRobLondonRob

      28.1k1676117




      28.1k1676117




      marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes


















          64














          It is a precedence operator issue.



          You should add extra parenthesis to make your multi condition test working:



          d[(d['x']>2) & (d['y']>7)]


          This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.






          share|improve this answer
































            1














            There may still be a better way, but



            In [56]: d[d['x'] > 2] and d[d['y'] > 7]
            Out[56]:
            x y
            4 5 8


            works.






            share|improve this answer


















            • 1





              this works, but ends up using python operators (rather than numpy) and so is going to be much slower

              – Jeff
              Jun 20 '13 at 14:44











            • that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

              – LondonRob
              Jun 20 '13 at 14:45











            • Oh, I've just found a duplicate of this question. Whoops.

              – LondonRob
              Jun 20 '13 at 14:48

















            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            64














            It is a precedence operator issue.



            You should add extra parenthesis to make your multi condition test working:



            d[(d['x']>2) & (d['y']>7)]


            This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.






            share|improve this answer





























              64














              It is a precedence operator issue.



              You should add extra parenthesis to make your multi condition test working:



              d[(d['x']>2) & (d['y']>7)]


              This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.






              share|improve this answer



























                64












                64








                64







                It is a precedence operator issue.



                You should add extra parenthesis to make your multi condition test working:



                d[(d['x']>2) & (d['y']>7)]


                This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.






                share|improve this answer















                It is a precedence operator issue.



                You should add extra parenthesis to make your multi condition test working:



                d[(d['x']>2) & (d['y']>7)]


                This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 15 '17 at 20:41









                A-B-B

                24.4k66470




                24.4k66470










                answered Jun 20 '13 at 14:43









                BoudBoud

                19.4k64057




                19.4k64057























                    1














                    There may still be a better way, but



                    In [56]: d[d['x'] > 2] and d[d['y'] > 7]
                    Out[56]:
                    x y
                    4 5 8


                    works.






                    share|improve this answer


















                    • 1





                      this works, but ends up using python operators (rather than numpy) and so is going to be much slower

                      – Jeff
                      Jun 20 '13 at 14:44











                    • that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

                      – LondonRob
                      Jun 20 '13 at 14:45











                    • Oh, I've just found a duplicate of this question. Whoops.

                      – LondonRob
                      Jun 20 '13 at 14:48















                    1














                    There may still be a better way, but



                    In [56]: d[d['x'] > 2] and d[d['y'] > 7]
                    Out[56]:
                    x y
                    4 5 8


                    works.






                    share|improve this answer


















                    • 1





                      this works, but ends up using python operators (rather than numpy) and so is going to be much slower

                      – Jeff
                      Jun 20 '13 at 14:44











                    • that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

                      – LondonRob
                      Jun 20 '13 at 14:45











                    • Oh, I've just found a duplicate of this question. Whoops.

                      – LondonRob
                      Jun 20 '13 at 14:48













                    1












                    1








                    1







                    There may still be a better way, but



                    In [56]: d[d['x'] > 2] and d[d['y'] > 7]
                    Out[56]:
                    x y
                    4 5 8


                    works.






                    share|improve this answer













                    There may still be a better way, but



                    In [56]: d[d['x'] > 2] and d[d['y'] > 7]
                    Out[56]:
                    x y
                    4 5 8


                    works.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 20 '13 at 14:33









                    TomAugspurgerTomAugspurger

                    16k35456




                    16k35456







                    • 1





                      this works, but ends up using python operators (rather than numpy) and so is going to be much slower

                      – Jeff
                      Jun 20 '13 at 14:44











                    • that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

                      – LondonRob
                      Jun 20 '13 at 14:45











                    • Oh, I've just found a duplicate of this question. Whoops.

                      – LondonRob
                      Jun 20 '13 at 14:48












                    • 1





                      this works, but ends up using python operators (rather than numpy) and so is going to be much slower

                      – Jeff
                      Jun 20 '13 at 14:44











                    • that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

                      – LondonRob
                      Jun 20 '13 at 14:45











                    • Oh, I've just found a duplicate of this question. Whoops.

                      – LondonRob
                      Jun 20 '13 at 14:48







                    1




                    1





                    this works, but ends up using python operators (rather than numpy) and so is going to be much slower

                    – Jeff
                    Jun 20 '13 at 14:44





                    this works, but ends up using python operators (rather than numpy) and so is going to be much slower

                    – Jeff
                    Jun 20 '13 at 14:44













                    that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

                    – LondonRob
                    Jun 20 '13 at 14:45





                    that's a nice solution. I like the fact that it explicitly uses and. Makes it clearer that there are two conditions being evaluated.

                    – LondonRob
                    Jun 20 '13 at 14:45













                    Oh, I've just found a duplicate of this question. Whoops.

                    – LondonRob
                    Jun 20 '13 at 14:48





                    Oh, I've just found a duplicate of this question. Whoops.

                    – LondonRob
                    Jun 20 '13 at 14:48



                    Popular posts from this blog

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme