Social Media Analytics on Trump and Biden's Twitter

-- by Charlie Chengrui Zheng 11/09/2020

In the recent presidential election, Joe Biden just defeated Donald Trump and became the next president of the United States. I am very interested in the statistics of their use of social media, especially Twitter. Therefore, I analyzed the data of their recent tweets by scraping their recent tweets, investigating people's responses and inspecting the contents of their tweets. Here is the detailed walkthrough of how this social media analytics was conducted.

Twitter Scraping

First, we need to scrape their tweets on Twitter. Because Twitter disallows web scrapers, you need apply to be a Twitter developer and get consumer key, consumer secret, access token and access token secret to be able to access the data of their twitter account. We will be using Tweepy to access the Twitter API.

Next, we can use the API we just built to query the Twitter library and scrape their tweets. We will be getting first 50 tweets according to the time line, by the time I wrote this article (The day after Biden declared his victory). Especially, we will be collecting the time stamp, the text of the tweet, the number of retweets and the number of likes of each tweet, among all the attributes of a tweet. Then, we store the tweets into dataframes.

Here is a small snippet of the first 10 tweets of biden's dataframe to showcase what we just scraped.

Analysis on Biden and Trump's tweets

Frequency

Let us see when the first and 50th tweets of Biden and Trump were sent

Let us see how often they tweeted.

According to statistics above, we can see that both 2 politicians were active on Twitter. They both tweeted at a staggering frequency but Trump is a even more active Twitter user.

Reweets and Favorites

We can visualize and compare the 2 presidents' numbers of retweets and favorites of each tweet, to observe how welcomed they are on Twitter.

The graphs show that Biden gained more favorites and retweets than Trump. On average, Biden has a little more retweets than Trump, and more than twice favorites than Trump. Biden is more popular than Trump on Twitter

Word Cloud

We will investigate the contents of the text of the 2 presidents' tweets. First, we will build a corpus for each president. The corpus contains every word in their tweets' text. We will first need to tokenize every tweets, and store them into the corpus and generate the visualized word cloud by using the function below. The word clouds will display the frequently mentioned words in their tweets. The bigger the word is, the more frequent the word is used.

Let us look at the word cloud of Trump's tweets (Note: 'rt' stands for 'retweet')

Let us look at the word cloud of Biden's tweets

Conclusion

From the analysis above, the statistics shows that, though Trump is more active on Twitter than Biden, Biden is more welcomed and more popular than Trump. The word clouds show that Trump cares more about the election by frequently mentioning "election" and "ballot", but Biden cares more about the country by frequently mentioning "nation", "us" and "America".