arame3333
arame3333

Reputation: 10213

Python; count the number of tweets retweeted

I have a Pandas dataframe containing tweets. I want to count the number of tweets that have been retweeted. This code does not work

tweets_retweeted = twitter.apply(lambda x:True if x.retweet_count > 0 else False)
count_of_tweets_retweeted = len(tweets_retweeted[tweets_retweeted == True].index)

The error message I get is

KeyError: ('retweet_count', 'occurred at index created_at')

Upvotes: 0

Views: 237

Answers (2)

Nikhil Gupta
Nikhil Gupta

Reputation: 1486

Without having the ability to recreate your example, there are a few things that could be going on.

  1. The error is likely coming from the 1st line where you are trying to access the column.
  2. You may be passing one column at a time to the apply function rather than one row at a time. Please use axis = 1 to pass each row to see if it works.

Also, just a best practice (in my humble opinion) is to not reference column names with the dot notation. Try to use the bracket notation to differentiate between column names and methods.

Upvotes: 1

oppressionslayer
oppressionslayer

Reputation: 7224

Can you do:

j = twitter['retweet_count'] > 0
j.value_counts()

Upvotes: 0

Related Questions