[英]How to check if any value of a column is in a range (in between two values) in Pandas?
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().
我得到的错误是
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
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!但我已经在使用
any()
了!
So what's the problem here?那么这里有什么问题呢? Why does it work with
==
but not with x<=v<=y
?为什么它适用于
==
但不适用于x<=v<=y
?
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
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 中满足条件的所有行。
You can use custom function for running on the dataframe:您可以使用自定义函数在数据框上运行:
df1 = pd.DataFrame({'a':[1,1,2,2], 'b':[1,1,2,2], 'c':[2,-1,4,5]})
myfun = lambda row: row['b']<=row['a']<=row['c']
df1['Result'] = df1.apply(myfun, axis=1)
display(df1)
如果您想查看其他列值,可以尝试
df.loc[ df.loc[:, 'columnX'].between(a, b), : ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.