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

基于例外列表选择性保留连字符的Pandas Series处理需求

Solution: Keep Specific Hyphenated Terms in a Pandas Series

Got it, let's work through how to preserve only the hyphenated terms from your list_to_keep while replacing every other hyphen with spaces in a Pandas Series. Here's a straightforward, reliable approach using regex and string manipulation:

Step-by-Step Breakdown & Code

The core idea is to temporarily "protect" the terms we want to keep, clean up all other hyphens, then restore the protected terms.

import pandas as pd
import re

# Your sample data and keep list
s = pd.Series(['do not-remove this-hyphen but remove-all of these-hyphens'])
list_to_keep = ['not-remove', 'this-hyphen']

# 1. Build a regex pattern to match exact terms from our keep list
# Use re.escape to handle any special characters that might be in the terms
keep_pattern = re.compile(r'\b(' + '|'.join(re.escape(term) for term in list_to_keep) + r')\b')

# 2. Replace the kept terms with a unique placeholder (pick something unlikely to exist in your data)
temp_placeholder = '___TEMP_HYPHEN_PROTECT___'
s_protected = s.str.replace(keep_pattern, temp_placeholder)

# 3. Replace all remaining hyphens with spaces
s_cleaned = s_protected.str.replace('-', ' ')

# 4. Restore the original kept terms by swapping the placeholder back
s_final = s_cleaned.str.replace(temp_placeholder, lambda match: list_to_keep[list_to_keep.index(match.group())])

# Check the final result
print(s_final)

Expected Output

0 do not-remove this-hyphen but remove all of these hyphens
dtype: object

Quick Notes on the Approach:

  • The \b in the regex ensures we match whole terms (so we don't accidentally partial-match similar phrases).
  • Using a unique placeholder keeps our target terms safe while we clean up other hyphens.
  • The lambda function in the final replace maps the placeholder back to the original term from our keep list seamlessly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:47:06