How to count @ of each row

Viewed 49

I want to count the numbers of tags ( like @mark ) in twitter dataset but the problem is that there is some @ without name ( @ ) or the name and the @ are separated ( @ mike ) that I don't want to count them.

NumberOfTags = df2['Tweet'].str.count('@')

the table :

idTweet tweet
1 @mike is my friend and @emilie is not
2 @ jordan look at this post and share it with @daniel @ @ @
1 Answers

pandas.Series.str.count supports regex syntax

df = pd.DataFrame.from_dict(
    {
        'Tweet': {
            1: '@mike is my friend and @emilie is not',
            2: '@ jordan look at this post and share it with @daniel @ @ @', }
    }
)

df['NumberOfTags'] = df['Tweet'].str.count('@\w+')

Result is

Tweet NumberOfTags
@mike is my friend and @emilie is not 2
@ jordan look at this post and share it with @daniel @ @ @ 1
Related