You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查DataFrame中每行所有值是否处于指定范围内?

如何检查DataFrame中的每一行,判断该行的所有值是否都处于指定的范围内?

示例数据

首先创建用于测试的DataFrame:

import pandas as pd

new = pd.DataFrame({'a': [1,2,3], 'b': [-5,-8,-3], 'c': [20,0,0]})

范围判断示例

指定范围为 [-5, 5] 时,各行的判断逻辑如下:

a  b   c
0  1 -5  20  # |20| > 5,不符合要求
1  2 -8   0  # |-8| > 5,不符合要求
2  3 -3   0  # |-3| ≤ 5,符合要求

迭代解决方案

通过iterrows()遍历每行,结合any()检查该行是否存在超出范围的值,最终输出每行的判断结果:

print(['不符合' if any(abs(i) > 5 for i in a) else '符合' for _, a in new.iterrows()])

# 输出结果:
# ['不符合', '不符合', '符合']

内容的提问来源于stack exchange,提问作者aeiou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:25:44