【发布时间】:2018-06-30 01:41:24
【问题描述】:
我是 python 新手,我发现我的很多问题已经得到解答。在编写各种语言的 7 年中,我从来没有真正在这里发布过问题,所以这次我真的很难过。
我正在使用 python 3.6
我有一个 pandas 数据框,其中有一列只是布尔值。我有一些代码,我只想在此列中的所有行都为 True 时执行。
在我使用过的代码中的其他地方:
if True not in df.column:
判断 df 中是否没有一行是 True。这很好用。
但由于某种原因,反过来不起作用:
if False not in df.column:
判断df中的所有行是否为真。
即使这样也返回 False:
import pandas as pd
S = pd.Series([True, True, True])
print(False not in S)
但我发现向系列中添加 .values 是双向的:
import pandas as pd
S = pd.Series([True, True, True])
print(False not in S.values)
我能想到的另一种选择是遍历列并使用 OR 运算符将每一行与初始化为 True 的变量进行比较。那么如果变量一直到最后都为真,那么一切都必须为真。
所以我的问题是:为什么这会返回 False?
import pandas as pd
S = pd.Series([True, True, True])
print(False not in S)
【问题讨论】:
-
我相信 DYZ 为您的问题提供了答案,而 GabrielA 为您想要实现的目标提供了解决方案。
标签: python pandas if-statement boolean logic