【问题标题】:Is there a way to remove 2 words from a sentence only if they occur consecutively?有没有办法只在连续出现的情况下从句子中删除 2 个单词?
【发布时间】:2021-11-25 08:00:12
【问题描述】:

仅当两个单词连续出现时,我才尝试从数据框列中删除单词。

text user
boy live on island user1
mall on the street boy live go user2

在上面的示例中,如果 boylive 连续出现,我想删除它们以获得如下输出:

text user
on island user1
mall on the street go user2

我正在使用 nltk 停用词来删除停用词。但我认为它只能包含单个单词。

【问题讨论】:

  • replace('boys live', '') 呢?

标签: python dataframe nlp nltk


【解决方案1】:

这是使用str.replace的方法之一

import pandas as pd
df = pd.DataFrame({
    'text': ['boy live on island', 'mall on the street boy live go'],
    'user': ['user1', 'user2']
})
df['text'] = df['text'].str.replace('boy live','')
print (df)

输出:

text   user
0               on island  user1
1  mall on the street  go  user2

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 2020-12-10
    • 1970-01-01
    • 2011-03-24
    • 2012-03-31
    相关资源
    最近更新 更多