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

创建新DataFrame时如何筛选members字段1到20的值而不使用|符号

问题解决方法

你要实现的是筛选members字段值在1到20范围内的行,不需要用多个|拼接条件,有两种常用的简洁写法:

方法1:使用between()方法(推荐用于连续区间判断)

between()是pandas专门用于区间判断的方法,默认包含左右边界,写法如下:

expeditions_success_20 = success_expedition[success_expedition['members'].between(1, 20)]

如果需要调整边界是否包含,可添加inclusive参数,可选值为both/neither/left/right,比如要左闭右开区间可以写为:

success_expedition[success_expedition['members'].between(1, 21, inclusive='left')]

方法2:使用isin()方法(适用于离散值集合判断)

如果你的需求是判断值是否属于1到20的所有整数,可以生成目标整数序列后用isin()匹配:

# range(1,21)会生成1到20的所有整数,range遵循左闭右开规则所以结束值填21
expeditions_success_20 = success_expedition[success_expedition['members'].isin(range(1, 21))]

原代码错误原因

==是等值判断运算符,只能用于和单个值、等长序列做匹配,不能直接识别1:20这类范围表达式,因此会抛出语法错误或不符合预期的结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 08:54:06