Twitter Data Cleaning using Python

Photo by MORAN on Unsplash

Twitter is one of the most used data sources for data analysis. The reason is that it’s open and free to collect unless you subscribe to the paid version one. Besides, it’s pretty simple to collect data from it. If you haven’t known how to collect Twitter data using python, you can check my previous post, teehee.

Twitter data contains a bunch of information parameters. Sometimes, the data contain unnecessary things that need to be cleaned, such as unnecessary characters, links, newlines, and other kinds of stuff. In this article, I’m going to show you how to clean Twitter data using the python programming language.

Firstly, you need to import the modules needed. We’re going to use 4 modules here:

  1. Pandas, to open data files and to apply certain operations to the data.
  2. Html, to decode HTML entities into regular characters.
  3. Re, to filter and delete unnecessary links, hash, username, punctuations or whatever you wish.
  4. Nltk, to clean stopwords.

Secondly, we need to import the Twitter data. in this case, I use CSV Twitter data, you may adjust the code if you use another extension type of file. We’re taking advantage of the pandas library here to import the data.

Once we have imported the data, we’re now ready for the data cleaning process. The first things that we’re going to clean are data duplicates. Most of the time, we don’t need the data duplicates, because in further use (i.e. analysis) these data duplicates could mess up the result by messing up the measurement.

If your dataframe have indices included on it, once you drop those data duplicates, you need to store the new dataframe in a new file. Don’t forget to store the new dataframe to a new file without including the index on it, so that we could explore the data more freely later on.

We’re here assuming that we’re only going to use the tweets data, so we’re going to extract the tweets data out of the file.

Once we extracted tweet data, we’ll notice things that need to be cleaned. Most of the time, the tweets returned by Twitter JSON data contain HTML entities and they need to be decoded into characters. So, we’re cleaning them using html library. Apart from that, we also need to clean up newlines since they make the data messy.

Sometimes when tweeting, Twitter users attach media like pictures, videos, etc. Those media will be converted into links on the JSON data. Since we’re only going to be using the text data, which is the tweets, so we need to clean up the links. Also, we will clean up hash characters (only the hash characters not the whole hashtags) and username. All those things will be cleaned using the regex Python library.

Up till now, we already got much cleaner data, but there is one more thing that we need to do to make it even cleaner. In text-data, mostly it contains insignificant words that are not used for the analysis process because they could mess up the analysis score. So, we’re about to clean them now using the nltk Python library. There are several steps you need to do to remove the stopwords:

- Preparing stopwords

- Tokenize the tweets

- Remove the Stopwords

So, that’s pretty much all about how to clean your Twitter data. I hope it was helpful for you. Thank you guys for reading. Bye for now and don’t forget to always keep an eye on your health. 👋🏻😉

〖A data geek 📊〗〖Life-long learner〗〖ESFP-T〗〖✨ŸØⱠØ✨〗

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store