Twitter Sentiment Analysis in Python
Calculating Subjectivity and Polarity Score of a Twitter Account

Let's load the config file (make sure you edit the config file and enter your Twitter API details as described in the introduction).
Config file config.csv has the following format:
We are making an authentication call with Tweepy so we can call a function to retrieve the latest tweets from the specified Twitter account.
We are using elonmusk Twitter account as an example but feel free to change the Tweeter account to a different one; even your own Twitter account.
Now we are going to retrieve the last 50 Tweets & replies from the specified Twitter account.
And we are going to create Pandas Data Frame from it.
Let's see what is in the Data Frame by calling the head() function.
And now we are going to apply it to all Tweets in our Pandas Data Frame.
We are also going to build a couple more functions to calculate the subjectivity and polarity of our tweets using TextBlob.
And now we are going to apply these functions to our Data Frame and create two new features in our Data Frame Subjectivity and Polarity.
Now, let's see how our data frame looks now.
And apply this function and create another feature in our Data Frame called Score.
Here is our Data Frame with our Tweets, Subjectivity, Polarity and Score for all our Tweets.
We can also calculate the percentage of objective tweets.

Doing sentiment analysis of the tweets enabled us to calculate numerical values of subjectivity and polarity.
This could help us to understand better this Twitter account in terms of the language that is being used.
Combining this with additional information about likes and comments can be very useful from a marketing point of view and can enable us to find some correlation between subjectivity, polarity and the engagement our the users for a specified Twitter account.
We encourage you to experiment more with this example and come up with some more ideas on how this can be used in practice.
If you would like to learn more and experiment with Python and Data Science you can look at another of my articles Analysing Pharmaceutical Sales Data in Python, Introduction to Computer Vision with MNIST and Image Face Recognition in Python.
To consolidate your knowledge consider completing the task again from the beginning without looking at the code examples and see what results you will get. This is an excellent thing to do to solidify your knowledge.
Full Python code in Jupyter Notebook is available on GitHub: https://github.com/pjonline/Basic-Data-Science-Projects/tree/master/8-Twitter-Sentiment-Analysis
Happy coding!
Not subscribing to Medium yet? Consider signing up to become a Medium member. It's only $5 a month and it will give you unlimited access to all stories on Medium. Subscribing to Medium supports me and other writers on Medium.








