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

如何用正则表达式过滤pandas.Series中仅含单个数字前缀的步骤字符串

过滤Pandas Series中以单个数字开头的字符串

原始数据

先定义目标Series:

import pandas as pd

s = pd.Series(['1-Onboarding + Retorno', '1.1-Onboarding escolha de bot',
               '2-Seleciona produto', '3-Informa localizacao e cpf',
               '3.1-CPF valido (V.2.0)', '3.2-Obtencao de CEP'], name='Steps')

需求

仅保留Series中以单个数字开头的字符串,排除以数字.数字格式开头的项。

解决方案

使用str.contains()配合正则表达式实现过滤,关键正则为^[0-9]-,各部分含义:

  • ^:锚定字符串的起始位置
  • [0-9]:匹配任意单个阿拉伯数字
  • -:匹配数字后的短横线,确保是一级步骤的格式

执行过滤的代码:

filtered_s = s[s.str.contains(r'^[0-9]-')]

过滤结果

运行后得到的结果:

0         1-Onboarding + Retorno
1            2-Seleciona produto
2    3-Informa localizacao e cpf
Name: Steps, dtype: object

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 05:16:35