为什么添加第二个条件时查询中断? -查询值超过2列的数据框

时间:2018-06-26 17:11:48

标签: python pandas dataframe

为什么我添加第二个条件时查询会中断?

只要我只查询一列,我的查询就起作用。当我尝试为数据框中的第二列添加查询时,它中断了。我正在使用python3和pandas。

我在“人员”列中返回所有带有“ MTC”的行。但是,当我添加“和”来缩小这些行的范围时,它变成了布尔条件,并且我收到一条错误消息。如何提取“人员”列中带有“ MTC”且在“ ExpNum”列中也包含3的所有行?

效果很好:

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]

不起作用:

print(df.loc [df ['Person'] =='MTC'和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().

我正在使用python 3

我认为错误消息告诉我我的查询是布尔值true false,并且它无法响应?

我应该如何更改代码,使其仅返回Person == MTC和ExpNum == 3且WL == 440的行?

0 个答案:

没有答案