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

如何在Pandas中提取Invoice与下一个空格之间的子字符串?

提取DataFrame中Invoice Number列指定内容的方法

可以使用pandas的str.extract()方法结合正则表达式实现需求,具体操作如下:

  • 正则表达式采用r'Invoice(\S+)',逻辑为:

    • Invoice:精准匹配固定起始字符串
    • (\S+):捕获Invoice之后所有非空白字符(直到遇到空格为止),这部分就是需要保留的目标内容
  • 处理代码示例:

import pandas as pd

# 构造示例DataFrame
df = pd.DataFrame({
    'Invoice Number': [
        'Being InvoiceNC/0095/2022.23 Dt. 06.10.2022',
        'Black Toner Cartridge agst InvoiceNC/0098/2022.23 dt 10.10.2022',
        'Being amount vide InvoiceS0001236 Dt. 03.10.2022',
        'Being amount vide InvoiceS0001235 Dt. 03.10.2022'
    ]
})

# 提取目标内容并覆盖原列
df['Invoice Number'] = df['Invoice Number'].str.extract(r'Invoice(\S+)', expand=False)

# 输出处理结果
print(df)

执行后得到的结果:

Invoice Number
0    NC/0095/2022.23
1    NC/0098/2022.23
2         S0001236
3         S0001235

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 11:05:23