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

如何基于给定筛选集合在pandas中正确应用数据过滤规则

Pandas数据过滤问题解决方案

错误原因

  • 第一处错误:首个过滤条件没有关联DataFrame列。你写的filter_names[0]==filter_values[0]是直接比较两个字符串常量"animal"和"cat",结果恒为False,无法实现对animal列的筛选,正确写法需要取df的对应列做判断:df[filter_names[0]] == filter_values[0]
  • 第二处错误:第二个过滤条件运算符与需求相反。需求要求列值小于筛选值,你使用了>=(大于等于)运算符,需要修改为<

修正后代码

df_filtered = df[(df[filter_names[0]] == filter_values[0]) & (df[filter_names[1]] < filter_values[1])]

运行输出

animal  age    name  visits priority
a    cat  2.5  Murzik       1      yes
f    cat  2.0   Vaska       3       no

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 14:54:03