'Removing @ mentions from pandas DataFrame column
I am working on a thesis project on smartworking. I downloaded some tweets using Python and I wanted to get rid of users / mentions before implementing wordclouds. However, I can't delete the users, but with the commands shown I delete only the "@".
df['token']=df['token'].apply(lambda x:re.sub(r"@mention","", x))
df['token']=df['token'].apply(lambda x:re.sub(r"@[A-Za-z0-9]+","", x))
Solution 1:[1]
Your second code should work, however for efficiency use str.replace:
df['token2'] = df['token'].str.replace('@[A-Za-z0-9]+\s?', '', regex=True)
# or for [a-zA-Z0-9_] use \w
# df['token2'] = df['token'].str.replace('@\w+\s?', '', regex=True)
example:
token token2
0 this is a @test case this is a case
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
