Python 为什么熊猫系列([True,True])中的False返回True?

Python 为什么熊猫系列([True,True])中的False返回True?,python,pandas,Python,Pandas,第一行代码返回False 第二行代码返回True 我想我一定是做错了什么或是错过了什么。当我检查序列是否有0时,我得到了同样的结果。有人能给我解释一下吗?您正在检查0(内部False表示)是否在系列的索引中-: 同: In [286]: False in pd.Series([True,True]) Out[286]: True In [288]: True in pd.Series([True,True]) Out[288]: True 及 In [287]: 0 in pd.Series

第一行代码返回False 第二行代码返回True


我想我一定是做错了什么或是错过了什么。当我检查序列是否有0时,我得到了同样的结果。有人能给我解释一下吗?

您正在检查
0
(内部
False
表示)是否在系列的索引中-:

同:

In [286]: False in pd.Series([True,True])
Out[286]: True
In [288]: True in pd.Series([True,True])
Out[288]: True

In [287]: 0 in pd.Series([True,True])
Out[287]: True
同:

In [286]: False in pd.Series([True,True])
Out[286]: True
In [288]: True in pd.Series([True,True])
Out[288]: True
但是

因为本系列中没有此类索引:

In [291]: 2 in pd.Series([True,True])
Out[291]: False
更新:

如果要检查是否至少有一个series元素为False或True:

In [292]: pd.Series([True,True])
Out[292]:
0    True
1    True
dtype: bool

如果要检查多个值,我强烈建议使用


听起来很合理,但我如何知道python是将第一个运算符与索引或值进行比较,还是python只是在这两个集合中搜索它?@mxdev,很可能您正在查找类似的内容:
(s==False)。any()
其中
s
是您的序列包含
True
还是
False
值或just
pd.series([True,True])。any()
(~pd.Series([True,True])。any()
:)@Mitch,是的,这是正确的,谢谢!我也想先使用这个简化版本,但我决定使用显式版本,因为它显示了它是如何工作的,它也适用于非布尔值。我刚刚做了一个在字符串序列中搜索字符串的测试,看起来它只在索引/aixforPd.series(['cd','ab'])中搜索x:打印x#它将打印cd和ab@mxdev
list(dict(pd.Series(['cd',ab']))
think dictionary(
keys
),而不是list。
In [296]: (pd.Series([True, True]) == False).any()
Out[296]: False

In [297]: (pd.Series([True, True]) == True).any()
Out[297]: True
>>> help(pd.Series([True, True]).__contains__)
Help on method __contains__ in module pandas.core.generic:

__contains__(key) method of pandas.core.series.Series instance
    True if the key is in the info axis

>>> pd.Series([True, True])
0    True
1    True
dtype: bool

^
info axis
>>> pd.Series(range(10)).isin([1]).any()
True