1

Explanation of why query breaks when I add a second condition?

My query works as long as I only query in one column. It breaks when I try to add a query for the second column in my dataframe. I am using python3 and pandas.

I get back all the rows with 'MTC' in my 'Person' column. However, when I add an 'and' to get narrow down those rows, it turns into a boolean condition and I get an error message. How can I pull out all the rows with 'MTC' in the 'Person' column, that also have 3 in the 'ExpNum' column?

Works great:

print(df.loc[df['Person'] == 'MTC'])

      OrigID Person       Date  Threshold RevNum   WL     RevPos  ExpNum  \
5032    3027    MTC  5/24/2018          0      1  440  16.881222       3   
5033    3032    MTC  5/24/2018          0      2  440  22.881222       3   
5034    3059    MTC  5/24/2018          0      3  440   6.431222       3   
5035    3064    MTC  5/24/2018          0      4  440  12.431222       3   
5036    3070    MTC  5/24/2018          0      5  440  12.431222       3   
5037    3072    MTC  5/24/2018          0      6  440  13.931222       3   
5038    3118    MTC  5/24/2018          0      7  440   5.731222       3   
5039    3119    MTC  5/24/2018          0      8  440   7.231222       3   
5040    3130    MTC  5/24/2018          0      9  440   2.311222       3   
5041    3131    MTC  5/24/2018          0     10  440   5.311222       3   
5042    3132    MTC  5/24/2018          0     11  440   5.311222       3   
5043    3133    MTC  5/24/2018          0     12  440   6.811222       3   
5044    3023    MTC  5/24/2018          0      1  452  26.063219       3   
5045    3026    MTC  5/24/2018          0      2  452  22.773219       3   
5046    3039    MTC  5/24/2018          0      3  452  40.773219       3   
5047    3127    MTC  5/24/2018          0      4  452  19.453219       3   
5048    3128    MTC  5/24/2018          0      5  452  20.953219       3   
5049    3135    MTC  5/24/2018          0      6  452  12.753219       3   
5050    3136    MTC  5/24/2018          0      7  452  14.253219       3   
5051    3137    MTC  5/24/2018          0      8  452  14.253219       3   
5052    3139    MTC  5/24/2018          0      9  452  15.753219       3   
5053    3140    MTC  5/24/2018          0     10  452  11.653219       3   
5054    3141    MTC  5/24/2018          0     11  452  13.153219       3   
5055    3142    MTC  5/24/2018          0     12  452   9.873219       3   
5056    3029    MTC  5/24/2018          0      1  464  18.288780       3   
5057    3034    MTC  5/24/2018          0      2  464  30.288780       3   
5058    3053    MTC  5/24/2018          0      3  464   0.000000       3   
5059    3057    MTC  5/24/2018          0      4  464   6.000000       3   
5060    3058    MTC  5/24/2018          0      5  464   6.000000       3   
5061    3061    MTC  5/24/2018          0      6  464   7.500000       3   
...      ...    ...        ...        ...    ...  ...        ...     ...   
6413    3208    MTC  5/24/2018          1      7  464 -23.402228       4   
6414    3215    MTC  5/24/2018          1      8  464 -24.902228       4   
6415    3250    MTC  5/24/2018          1      9  464 -20.802228       4   
6416    3251    MTC  5/24/2018          1     10  464 -22.302228       4   
6417    3256    MTC  5/24/2018          1     11  464 -21.482228       4   
6418    3257    MTC  5/24/2018          1     12  464 -24.482228       4   
6419    3156    MTC  5/24/2018          1      1  476 -19.485844       4   
6420    3157    MTC  5/24/2018          1      2  476 -25.485844       4   
6421    3160    MTC  5/24/2018          1      3  476 -22.195844       4   
6422    3161    MTC  5/24/2018          1      4  476 -28.195844       4   
6423    3188    MTC  5/24/2018          1      5  476 -24.915844       4   
6424    3196    MTC  5/24/2018          1      6  476 -26.415844       4   
6425    3204    MTC  5/24/2018          1      7  476 -26.415844       4   
6426    3211    MTC  5/24/2018          1      8  476 -29.415844       4   
6427    3230    MTC  5/24/2018          1      9  476 -29.415844       4   
6428    3241    MTC  5/24/2018          1     10  476 -30.915844       4   
6429    3248    MTC  5/24/2018          1     11  476 -30.095844       4   
6430    3252    MTC  5/24/2018          1     12  476 -33.095844       4   
6431    3150    MTC  5/24/2018          1      1  488 -28.084528       4   
6432    3153    MTC  5/24/2018          1      2  488 -24.794528       4   
6433    3154    MTC  5/24/2018          1      3  488 -30.794528       4   
6434    3167    MTC  5/24/2018          1      4  488 -28.334528       4   
6435    3172    MTC  5/24/2018          1      5  488 -29.834528       4   
6436    3176    MTC  5/24/2018          1      6  488 -29.014528       4   
6437    3191    MTC  5/24/2018          1      7  488 -35.014528       4   
6438    3203    MTC  5/24/2018          1      8  488 -34.194528       4   
6439    3205    MTC  5/24/2018          1      9  488 -35.694528       4   
6440    3213    MTC  5/24/2018          1     10  488 -34.054528       4   
6441    3219    MTC  5/24/2018          1     11  488 -35.554528       4   
6442    3221    MTC  5/24/2018          1     12  488 -35.554528       4   

         ThExp  
5032  Inc-Exp3  
5033  Inc-Exp3  
5034  Inc-Exp3  
5035  Inc-Exp3  
5036  Inc-Exp3  
5037  Inc-Exp3  
5038  Inc-Exp3  
5039  Inc-Exp3  
5040  Inc-Exp3  
5041  Inc-Exp3  
5042  Inc-Exp3  
5043  Inc-Exp3  
5044  Inc-Exp3  
5045  Inc-Exp3  
5046  Inc-Exp3  
5047  Inc-Exp3  
5048  Inc-Exp3  
5049  Inc-Exp3  
5050  Inc-Exp3  
5051  Inc-Exp3  
5052  Inc-Exp3  
5053  Inc-Exp3  
5054  Inc-Exp3  
5055  Inc-Exp3  
5056  Inc-Exp3  
5057  Inc-Exp3  
5058  Inc-Exp3  
5059  Inc-Exp3  
5060  Inc-Exp3  
5061  Inc-Exp3  
...        ...  
6413  Dec-Exp4  
6414  Dec-Exp4  
6415  Dec-Exp4  
6416  Dec-Exp4  
6417  Dec-Exp4  
6418  Dec-Exp4  
6419  Dec-Exp4  
6420  Dec-Exp4  
6421  Dec-Exp4  
6422  Dec-Exp4  
6423  Dec-Exp4  
6424  Dec-Exp4  
6425  Dec-Exp4  
6426  Dec-Exp4  
6427  Dec-Exp4  
6428  Dec-Exp4  
6429  Dec-Exp4  
6430  Dec-Exp4  
6431  Dec-Exp4  
6432  Dec-Exp4  
6433  Dec-Exp4  
6434  Dec-Exp4  
6435  Dec-Exp4  
6436  Dec-Exp4  
6437  Dec-Exp4  
6438  Dec-Exp4  
6439  Dec-Exp4  
6440  Dec-Exp4  
6441  Dec-Exp4  
6442  Dec-Exp4  

[283 rows x 9 columns]

Does not work:

print(df.loc[df['Person'] == 'MTC' and df['ExpNum'] == '3'])

ValueError                                Traceback (most recent call last)
<ipython-input-32-3a09e9a5facb> in <module>()
----> 1 print(df.loc[df['Person'] == 'MTC' and df['ExpNum'] == '3'])

c:\users\rijekah\appdata\local\programs\python\python35\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1571         raise ValueError("The truth value of a {0} is ambiguous. "
   1572                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1573                          .format(self.__class__.__name__))
   1574 
   1575     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I am using python 3

I think the error message is telling me that my query is a boolean true false and that it cannot respond??

How should I change my code so that it only brings back rows where Person == MTC and ExpNum == 3 and WL == 440 ?

Rebecca Ijekah
  • 429
  • 2
  • 5
  • 14
  • two things Use `&` not `and` and use parentheses `()`. `df.loc[(df['Person'] == 'MTC') & (df['ExpNum'] == '3')])` – piRSquared Jun 26 '18 at 17:13
  • I used & and parentheses and it still does not work? I also do not understand the explanation in the duplicate question link at the top. (says duplicate question) print(df.loc[(df['Person'] == 'MTC') & (df['ExpNum'] == '3')])) – Rebecca Ijekah Jun 27 '18 at 17:01
  • One of the issues I was having was that a column I was trying to pull from was datatype object and I thought it was a datatype integer. I had to remove a few rows containing strings in that column, and then convert to integer, then it worked. – Rebecca Ijekah Jun 28 '18 at 21:24

0 Answers0