为什么pandas.Series([True,True])中的False返回True? [英] Why does `False in pandas.Series([True,True])` return True?

查看:819
本文介绍了为什么pandas.Series([True,True])中的False返回True?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

False in [True,True]

False in pd.Series([True,True])

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

the first line of code returns False the second line of code returns True!

我想我一定是在做错什么或在这里错过了什么.当我检查序列是否为0时,我会得到相同的结果.有人可以向我解释一下吗?

I think I must be doing something wrong or missing something here. I get the same thing when I check if the series has 0. Can someone please explain this to me?

推荐答案

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

You are checking whether 0 (internal False representation) is in the Series's index - @Uriel has shown a docstring explaining why is it happening:

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

与以下相同:

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

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

与以下相同:

In [290]: 1 in pd.Series([True,True])
Out[290]: True

但是

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

因为在本系列中没有这样的索引:

because there is no such index in this series:

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

更新:

如果要检查至少一个系列元素是False还是True:

if you want to check whether at least one series element is False or True:

In [296]: (pd.Series([True, True]) == False).any()
Out[296]: False

In [297]: (pd.Series([True, True]) == True).any()
Out[297]: True

这篇关于为什么pandas.Series([True,True])中的False返回True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆