<p>我有一个数据帧<code>df</code>,其中包含未清理的文本字符串</p>
<pre><code> phrase
0 the quick brown br fox
1 jack and jill went up the hill
</code></pre>
<p>我还有一个单词和字母分组的列表,我想<code>remove</code>称为remove,如下所示:</p>
<pre><code>['br', and]
</code></pre>
<p>在本例中,我希望得到以下输出:</p>
<pre><code> phrase
0 the quick brown fox
1 jack jill went up the hill
</code></pre>
<p>请注意,“brown”中的<code>br</code>不是作为一个较大单词的一部分保留在<code>df</code>中,但是“br”本身被删除了</p>
<p>我试过:</p>
<pre><code>df['phrase']=[re.sub(r"\b%remove\b", "", sent) for sent in df['phrase']]
</code></pre>
<p>但不能让它正常工作。有人能告诉我怎么做吗</p>
<p>谢谢</p>
<p>我觉得它可以随着<code>replace</code>下降</p>
<pre><code>s=[r'\b'+x+r'\b' for x in L]
df.phrase.str.replace('|'.join(s),'')
Out[176]:
0 the quick brown fox
1 jack jill went up the hill
Name: phrase, dtype: object
</code></pre>