我想從我的 Pandas 資料框列中每個單詞的末尾洗掉字母 br(如您所見,該列的行實際上是句子 - 彼此不同)。
不幸的是,我已經清理了資料而沒有過多考慮 <br> 標簽,所以我現在只剩下諸如“startbr”、“nicebr”和“hellobr”之類的詞,這些詞對我來說毫無用處。
資料框行可能看起來像這樣(由 ** ** 標簽表示的錯誤):
Sentence = here are **somebr** examples of poorly written paragraphs **andbr** well-written **paragraphsbr** on the same **topicbr** how do they compare?
我想要什么(最后沒有 br):
Sentence: here are **some** examples of poorly written **and** well-written **paragraphs** on the same **topic** how do they compare?
我希望我的答案能讓我保留原來的句子(沒有任何單詞后面跟著字母 br)。像“野蠻”、“令人嘆為觀止”和“余燼”這樣的詞應該保持原樣,因為它們可能很有價值。幸運的是,沒有任何單詞我想以字母 br 結尾。
uj5u.com熱心網友回復:
使用帶有單詞邊界 ( \b) 的正則運算式來匹配單詞的結尾:
df['text'] = df['text'].str.replace(r'br\b', '', regex=True)
示例(將分配作為新列text2):
text text2
0 word wordbr bread breadbr word word bread bread
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/522300.html
