如何删除python中与特殊字符连接的单词

2024-09-28 23:28:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有你的理由!如何删除包含@的所有单词,例如@AmericanVirgin。当我这样做的时候

df['text'] = df ['text'].Str.replace('@', '') only removes @

这是我的数据帧:

weet_id airline_sentiment   text    Raiting
0   570306133677760513  neutral @VirginAmerica What @dhepburn said. 2
1   570301130888122368  positive    @VirginAmerica plus you've added commercials t...   3
2   570301083672813571  neutral @VirginAmerica I didn't today... Must mean I n...   2

这是我的回答 enter image description here


Tags: 数据textidonlydf单词replace理由
1条回答
网友
1楼 · 发布于 2024-09-28 23:28:00

输入:

tweet_id             airline sentiment  \
0         0  570306133677760513   neutral   
1         1  570301130888122368  positive   
2         2  570301083672813571   neutral   

                                          text  Raiting  \
0           @VirginAmerica What @dhepburn said        2   
1  @VirginAmerica plus youve added commercials        3   
2                @VirginAmerica I didn't today        0   
# Using regex, removing words starting with @
dframe["text_corrected"] = dframe.text.str.replace("@\w+\s", "")

输出:

tweet_id             airline sentiment  \
0         0  570306133677760513   neutral   
1         1  570301130888122368  positive   
2         2  570301083672813571   neutral   

                                          text  Raiting  \
0           @VirginAmerica What @dhepburn said        2   
1  @VirginAmerica plus youve added commercials        3   
2                @VirginAmerica I didn't today        0   

                  text_corrected  
0                     What  said  
1   plus youve added commercials  
2                 I didn't today  

相关问题 更多 >