How to check if any value of a column is in a range (in between two values) in Pandas?

心不动则不痛 提交于 2019-12-07 01:36:53

问题


I have a DataFrame and I would like to check if any of the values (v) of a column satisfies 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!

So what's the problem here? Why does it work with == but not with x<=v<=y?


回答1:


Use between to do this, it also supports whether the range values are included or not via 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:

In [132]:
s.between(0,1).any()

Out[132]:
True



回答2:


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.




回答3:


If you like to see other column values, you could try

df.loc[ df.loc[:, 'columnX'].between(a, b), : ]


来源:https://stackoverflow.com/questions/40156469/how-to-check-if-any-value-of-a-column-is-in-a-range-in-between-two-values-in-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!