如何检查列的任何值是否在 Pandas 的范围内(在两个值之间)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40156469/
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
How to check if any value of a column is in a range (in between two values) in Pandas?
提问by alec_djinn
I have a DataFrame and I would like to check if any of the values (v) of a column satisfies x<=v<=y
.
我有一个 DataFrame,我想检查列的任何值 (v) 是否满足x<=v<=y
.
equal = any(df['columnX'] == value) # No problems here
in_between = any(x <= df['columnX'] <= y) # ValueError :/
The error I get is ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
But I am using any()
already!
我得到的错误是ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
但我any()
已经在使用了!
So what's the problem here? Why does it work with ==
but not with x<=v<=y
?
那么这里有什么问题呢?为什么它适用于==
但不适用x<=v<=y
?
回答by EdChum
Use between
to do this, it also supports whether the range values are included or not via inclusive
arg:
使用between
要做到这一点,它也支持是否包括范围内的值或不通过inclusive
ARG:
In [130]:
s = pd.Series(np.random.randn(5))
s
Out[130]:
0 -0.160365
1 1.496937
2 -1.781216
3 0.088023
4 1.325742
dtype: float64
In [131]:
s.between(0,1)
Out[131]:
0 False
1 False
2 False
3 True
4 False
dtype: bool
You then call any
on the above:
然后你调用any
上面的:
In [132]:
s.between(0,1).any()
Out[132]:
True
回答by Ulf Aslak
You can just have two conditions:
你可以只有两个条件:
df[(x <= df['columnX']) & (df['columnX'] <= y)]
This line will select all rows in df where the condition is satisfied.
此行将选择 df 中满足条件的所有行。
回答by Cloud Cho
If you like to see other column values, you could try
如果您想查看其他列值,可以尝试
df.loc[ df.loc[:, 'columnX'].between(a, b), : ]