VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-remove-repetitive-characters-from-words-of-the-given-pandas-dataframe-using-regex/

⇱ How to Remove repetitive characters from words of the given Pandas DataFrame using Regex? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Remove repetitive characters from words of the given Pandas DataFrame using Regex?

Last Updated : 15 Jul, 2025

Prerequisite: Regular Expression in Python

In this article, we will see how to remove continuously repeating characters from the words of the given column of the given Pandas Dataframe using Regex.

Here, we are actually looking for continuously occurring repetitively coming characters for that we have created a pattern that contains this regular expression (\w)\1+ here \w is for character, 1+ is for the characters that come more than once. 

We are passing our pattern in the re.sub() function of re library. 

Syntax: re.sub(pattern, repl, string, count=0, flags=0)

The ‘sub’ in the function stands for SubString, a certain regular expression pattern is searched in the given string(3rd parameter), and upon finding the substring pattern is replaced by repl(2nd parameter), count checks and maintains the number of times this occurs. 

Now, Let's create a Dataframe:

 
 

Output:


 

👁 Image


 


Now, Remove continuously repetitive characters from words of the Dataframe common_comments column. 


 

 
 

Output:


 

👁 Image


Time Complexity : O(n) where n is the number of elements in the dataframe.

Space complexity : O(m * n) where m is the number of columns and n is the number of elements in the dataframe

Comment