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

Pandas DataFrame中字符串按"|||"分割时出现逐字符拆分的问题求助

Fixing Pandas str.split() Splitting by Character Instead of "|||"

Ah, I know exactly what's going on here—you ran into a classic regex gotcha with Pandas' string methods! Let's break this down and fix it step by step.

Why It's Splitting by Character

The issue is that str.split() in Pandas uses regular expressions by default, and the pipe character | is a special regex metacharacter that means "OR". When you pass '|||' as the separator, regex interprets it as "empty string OR empty string OR empty string"—which effectively splits the string at every single character. That's why you're seeing that weird character-by-character split result.

Two Simple Solutions

1. Disable Regex Matching (Easiest Fix)

Pandas added the regex=False parameter specifically for cases like this, where you want to split on a literal string without treating it as regex. Just add this parameter to your code:

df['query_ids'].str.split('|||', n=5, expand=True, regex=False)

This tells Pandas to match the exact string "|||" instead of parsing it as a regex pattern—no extra escaping needed.

2. Escape the Special Regex Characters

If you need to keep regex functionality (for example, if you might use more complex patterns later), you can escape each | with a backslash. Since backslashes are escape characters in Python strings, you have two options:

  • Use a raw string prefix r to avoid double-escaping:
    df['query_ids'].str.split(r'\|\|\|', n=5, expand=True)
    
  • Or double-escape each | in a regular string:
    df['query_ids'].str.split('\\|\\|\\|', n=5, expand=True)
    

Test It Out

Let's verify with your sample string to make sure it works:

import pandas as pd

sample_query = 'intermediation|"mechanical turk"|precarious "public policy" ||| intermediation|"mechanical turk"|precarious high-level'
df = pd.DataFrame({'query_ids': [sample_query]})

# Using regex=False
result = df['query_ids'].str.split('|||', n=5, expand=True, regex=False)
print(result.iloc[0])

This should output two columns (since there's one "|||" separator) with the two distinct content blocks you expect.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:52:44