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

如何在Pandas中移除单元格开头的‘n+ years in business’前缀并保留无该前缀的行

在Pandas中移除地址单元格的业务年限前缀,保留原始地址

我刚好有过类似的处理经验,针对你这种混合了带前缀和纯地址的列数据,用正则表达式配合Pandas的str.replace方法就能完美解决——既能精准移除那些n+ years in business · 格式的前缀,又不会改动原本就没有前缀的地址行。

具体实现代码

假设你的数据列名为address,可以直接用这一行代码处理:

import pandas as pd

# 假设你的DataFrame名为df
df['address'] = df['address'].str.replace(r'^\d+\+ years in business · ', '', regex=True)

代码解释

这个正则表达式r'^\d+\+ years in business · '的作用是:

  • ^:匹配字符串的开头,确保我们只处理前缀在最前面的情况
  • \d+:匹配一个或多个数字(对应前缀里的n,比如5、7、10)
  • \+:匹配实际的加号(因为加号在正则里是特殊字符,需要用反斜杠转义)
  • 后面的years in business ·就是你要移除的固定文本部分

替换为空字符串后,只有匹配到这个前缀的单元格会被移除前缀,原本没有前缀的地址会完全保留原样。

处理效果示例

针对你给出的原始数据:

  • 原始:10+ years in business · Bypass Rd, behind ST. Conrad's College → 处理后:Bypass Rd, behind ST. Conrad's College
  • 原始:5+ years in business · Ground Floor, 84 → 处理后:Ground Floor, 84
  • 原始:1st Floor SK Complex, Madison square Rd near C.O. County Office, near Church → 处理后:1st Floor SK Complex, Madison square Rd near C.O. County Office, near Church(完全不变)
  • 原始:7+ years in business · David Rd 80A, Henry Palace, Phase 1, Ronda Rd → 处理后:David Rd 80A, Henry Palace, Phase 1, Ronda Rd

这样就完全满足你的需求啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:57:45