Python 检查熊猫系列是否至少有一项大于某个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34148815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:31:50  来源:igfitidea点击:

Check if a pandas Series has at least one item greater than a value

pythonpandas

提问by ChaimG

The following code will print True because the Series contains at least one element that is greater than 1. However, it seems a bit un-Pythonic. Is there a more Pythonic way to return True if a Series contains a number that is > a particular value?

以下代码将打印 True,因为该系列包含至少一个大于 1 的元素。但是,它似乎有点非 Pythonic。如果系列包含大于特定值的数字,是否有更 Pythonic 的方式返回 True?

import pandas as pd

s = pd.Series([0.5, 2])
print True in (s > 1)

True

真的

EDIT: Not only is the above answer un-Pythonic, it will sometimes return an incorrect result for some reason. For example:

编辑:上述答案不仅非 Pythonic,而且有时会由于某种原因返回不正确的结果。例如:

s = pd.Series([0.5])
print True in (s < 1)

False

错误的

采纳答案by Anton Protopopov

You could use anymethod to check if that condition is Trueat least for the one value:

您可以使用any方法来检查该条件是否True至少适用于一个值:

In [36]: (s > 1).any()
Out[36]: True