remove words starting with "@" in a column from a dataframe

Viewed 1254

I have a dataframe called tweetscrypto and I am trying to remove all the words from the column "text" starting with the character "@" and gather the result in a new column "clean_text". The rest of the words should stay exactly the same:

enter image description here

tweetscrypto['clean_text'] = tweetscrypto['text'].apply(filter(lambda x:x[0]!='@', x.split()))

it does not seem to work. Can somebody help?

Thanks in advance

2 Answers

Please str.replace string starting with @

Sample Data

                                       text
0  News via @livemint: @RBI bars banks from links
1      Newsfeed from @oayments_source: How Africa
2                   is that bitcoin? not my thing


 tweetscrypto['clean_text']=tweetscrypto['text'].str.replace('(\@\w+.*?)',"")

Still, can capture @ without escaping as noted by @baxx

tweetscrypto['clean_text']=tweetscrypto['text'].str.replace('(@\w+.*?)',"")

                    clean_text
0  News via :  bars banks from links
1         Newsfeed from : How Africa
2      is that bitcoin? not my thing

In this case it might be better to define a method rather than using a lambda for mainly readability purposes.

def clean_text(X):
    X = X.split()
    X_new = [x for x in X if not x.startswith("@")
    return ' '.join(X_new)

tweetscrypto['clean_text'] = tweetscrypto['text'].apply(clean_text)
Related