You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何使用正则去除DataFrame标点?解决Python文本清洗后的标点残留问题

解决推文清洗脚本残留标点及DataFrame标点去除问题

一、修复推文清洗脚本的残留标点问题

你的脚本里残留标点(比如''')主要有两个原因:

  • string.punctuation只包含基础标点,像特殊单引号、连续单引号这类不在默认集合里;
  • 停用词处理的时机和方式有点冗余,可能导致部分标点碎片没被清理。

下面是修复后的完整代码,我会标注关键改进点:

import re
from collections import Counter
from string import punctuation
import nltk
# 第一次运行需要下载停用词库,之后可以注释掉
nltk.download('stopwords')
from nltk.corpus import stopwords

def processTweet(tweet):
    '''
    parameters:
    ====================
    - tweet: str (单条文本,原注释误写为list,已修正)
    functions:
    ====================
    - Remove HTML special entities (e.g. &)
    - Remove @username (原注释写转AT_USER,但代码是直接删除,保持原逻辑)
    - Remove tickers
    - Convert to lowercase
    - Remove hyperlinks
    - Remove hashtags
    - Remove punctuation and split contractions for filtering
    - Remove stopwords
    - Remove words with 2 or fewer letters
    - Remove extra whitespace
    - Remove leading space
    '''
    # Remove HTML special entities
    tweet = re.sub(r'\&\w*;', '', tweet)
    # Remove @username
    tweet = re.sub('@[^\s]+','',tweet)
    # Remove tickers
    tweet = re.sub(r'\$\w*', '', tweet)
    # Convert to lowercase
    tweet = tweet.lower()
    # Remove hyperlinks
    tweet = re.sub(r'https?:\/\/.*\/\w*', '', tweet)
    # Remove hashtags
    tweet = re.sub(r'#\w*', '', tweet)
    
    # 关键改进1:扩展标点集合,覆盖特殊引号、连续单引号、省略号
    extended_punctuation = punctuation + "’‘''…"
    # 用re.escape避免标点中的正则特殊字符出错,替换所有标点为空格
    tweet = re.sub(r'[' + re.escape(extended_punctuation) + ']+', ' ', tweet)
    
    # 关键改进2:优化停用词处理,去掉重复的lower()调用
    tweet_words = tweet.split()
    tweet_words = [word for word in tweet_words if word not in stopwords.words("english")]
    tweet = " ".join(tweet_words)
    
    # Remove short words (1-2 letters)
    tweet = re.sub(r'\b\w{1,2}\b', '', tweet)
    # Remove extra whitespace
    tweet = re.sub(r'\s\s+', ' ', tweet)
    # Remove leading space
    tweet = tweet.lstrip(' ')
    
    return tweet

关键改进说明

  1. 补充停用词库依赖:原代码缺少nltk停用词的导入和下载步骤,这会直接导致报错,现在补上了必要的依赖处理;
  2. 扩展标点覆盖范围:把易残留的特殊标点加入集合,确保所有冗余标点都被替换;
  3. 优化停用词处理逻辑:去掉重复的lower()调用,因为前面已经把整个文本转成小写了,提升效率;
  4. 正则转义处理:用re.escape()包裹标点集合,避免标点中的正则特殊字符(比如-[])导致匹配出错。

二、用正则去除DataFrame中的标点

针对DataFrame的标点清洗,分两种场景处理:

场景1:清洗整个DataFrame的所有字符串列

applymap()遍历每个单元格,只处理字符串类型的内容,不影响数值、日期等其他类型:

import pandas as pd

def remove_punctuation(text):
    if isinstance(text, str):
        extended_punctuation = punctuation + "’‘''…"
        return re.sub(r'[' + re.escape(extended_punctuation) + ']+', '', text)
    # 非字符串类型直接返回原内容
    return text

# 假设你的DataFrame名为df
df_cleaned = df.applymap(remove_punctuation)

场景2:只清洗特定文本列(比如名为content的列)

str.replace()更高效,专门针对字符串列处理:

extended_punctuation = punctuation + "’‘''…"
df['content_cleaned'] = df['content'].str.replace(r'[' + re.escape(extended_punctuation) + ']+', '')

如果想更简洁,也可以用正则匹配所有非字母数字、非空格的字符来快速去除所有标点:

df['content_cleaned'] = df['content'].str.replace(r'[^\w\s]', '', regex=True)

注意:这个正则会去掉所有非单词(字母、数字、下划线)和非空格的字符,如果你的文本里有需要保留的特殊字符,还是用扩展标点集合的方式更稳妥。

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

火山引擎 最新活动