Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
course content

Course Content

Python for Data Science: Tweet Sentiment Analysis

Text CleaningText Cleaning

Cleaning text in Python refers to the process of preparing raw text data for further analysis or modeling. The goal of text cleaning is to remove or correct errors, inconsistencies, and irrelevant information in the text so that it can be used effectively in an analysis or model.

There are several common steps involved in text cleaning, these include:

  • Removing punctuation: This step involves removing any punctuation marks, such as periods, commas, and exclamation marks, that may be present in the text;
  • Removing numbers: This step involves removing any numerical digits that may be present in the text;
  • Removing special characters: This step involves removing any special characters, such as "@" and "#", that may be present in the text;
  • Removing white spaces: This step involves removing any extra white spaces that may be present in the text;
  • Removing stopwords: This step involves removing commonly used words, such as "the", "is", "and", that may be present in the text, stopwords are often considered irrelevant for the analysis;
  • Lowercasing the text: This step involves converting all the text to lowercase, this is useful when working with text data because words that have the same meaning but are written in uppercase or lowercase are considered different;
  • Tokenization: This step involves breaking the text into smaller units, such as words or sentences, to make it easier to analyze;
  • Stemming/Lemmatization: This step involves reducing words to their root form, this can be useful when you want to group words that have the same meaning.

Methods description

  • import re: This imports the Python regular expression module, which provides functions for working with regular expressions;
  • import string: This imports the string module, which provides a collection of string constants and functions for working with strings;
  • data[column_name]: This indexes the DataFrame data to access the column_name column;
  • data[column_name].apply(lambda x: clean_text(x)): This applies the clean_text function to the column_name column of the DataFrame data using the apply method.


Clean the text from any extra character by running the clean_text function on the "text" and "selected_text" columns.

Mark tasks as Completed

Everything was clear?

Section 1. Chapter 7