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

技术求助:移除DataFrame列中分号";"后的空格

Solution for Removing Spaces After Semicolons in a DataFrame Column

Got it, let's fix this problem where you need to strip only the spaces that come right after semicolons (;) in a DataFrame column. This is straightforward with pandas' string methods and regular expressions—here are a couple of tailored approaches:

1. Remove All Consecutive Spaces After Semicolons

If you want to eliminate one or more spaces immediately following a semicolon (e.g., turning "; hello" into ";hello"), use this regex-based replacement:

import pandas as pd

# Example DataFrame
df = pd.DataFrame({
    'content': [
        'Apple ;  Banana',
        'Cat; Dog',
        'Hello ;   World!',
        'Test ;'
    ]
})

# Apply the replacement
df['content'] = df['content'].str.replace(r';\s+', ';', regex=True, na=False)

What this does:

  • r';\s+': The regex pattern matches a semicolon followed by one or more whitespace characters (spaces, tabs, etc.). If you only want to target spaces (not tabs), use r'; +' instead.
  • na=False: Ensures we don't throw errors if there are NaN values in the column.

After running this, your content column will look like:

  • Apple;Banana
  • Cat;Dog
  • Hello;World!
  • Test;

2. Remove Only the First Space After Semicolons

If you want to keep spaces that come after the first one post-semicolon (e.g., turning "; hello there" into ";hello there"), use a simpler pattern:

df['content'] = df['content'].str.replace(r';\s', ';', regex=True, na=False)

This targets exactly one whitespace character right after a semicolon, leaving any subsequent spaces intact.

Why this works better than generic replacements

Unlike broad string stripping or splitting, this regex approach is precise—it only modifies the specific part of the text you care about, leaving all other spaces and punctuation untouched.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:58:55