简体   繁体   English

如何检查一列的任何值是否在 Pandas 的范围内(在两个值之间)?

[英]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), : ]

Another way is to use query :另一种方法是使用query

In [2]: df = pd.DataFrame({'num': range(0, 5), 'alpha': list('ABCDE')})

In [3]: df
Out[3]: 
   num alpha
0    0     A
1    1     B
2    2     C
3    3     D
4    4     E

In [4]: df.query('1 <= num <= 3')
Out[4]: 
   num alpha
1    1     B
2    2     C
3    3     D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查熊猫中列值之间的差异? - How to check differences between column values in pandas? 熊猫如何检查列值之间的差异是否在每个组中的范围内 - pandas how to check differences between column values are within a range or not in each group 如何检查特定列中的范围是否涵盖 Pandas 中的特定值? - How to check if a range within a specific column covers a specific value in Pandas? 根据列值计算两个熊猫数据框之间的值 - calculate values between two pandas dataframe based on a column value 如果值不在同一列上的两个其他值之间,则删除 pandas 行 - Drop pandas rows if value is not between two other values on the same column 检查值是否在两个值之间 pandas - check if values are between two values pandas 熊猫,如何检查哪些 date_range 值在 pd.Interval 列的时间范围内 - Pandas, how to check which date_range values are in pd.Interval column's time range 如何按两列值之间的行对熊猫数据框进行切片? - How to slice a pandas dataframe by rows between two column values? Pandas:计算一列中两个值之间的行数 - Pandas: count how many rows between two values in a column 如何检查两个数据帧(Pandas)中多列中的列值? - How to check column value in multiple columns in two dataframes (Pandas)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM