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

使用pandas的filter方法时如何排除指定字符串模式的列

方法1:使用filter搭配正则实现

你之前的写法无法生效是因为正则语法不支持直接用&做逻辑且匹配,需要用负向先行断言实现排除规则:

# 正则规则:排除开头为ipo_date的列,匹配所有以_date结尾的列
df_filtered = df.filter(regex=r'^(?!ipo_date).*_date$')

方法2:使用列表推导式筛选(可读性更高)

如果不想处理复杂正则,可以直接遍历列名做逻辑判断,维护起来更简单:

# 同时满足两个筛选规则:列名包含_date、且列名不等于ipo_date
selected_cols = [col for col in df.columns if '_date' in col and col != 'ipo_date']
df_filtered = df[selected_cols]

两种方法都可以得到仅包含l2y_gg_date、l1k_kk_date两列的结果。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 23:36:04