Pandas Example – Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame

Hits: 10

(Python Example for Beginners)

 

Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame.

 

Sample Solution:

Python Code :


import pandas as pd
import re as re

pd.set_option('display.max_columns', 10)

df = pd.DataFrame({
    'text_code': ['t0001.','t0002','t0003', 't0004'],
    'text_lang': ['She livedd a long life.', 'How oold is your father?', 'What is tthe problem?','TThhis desk is used by Tom.']
    })

print("Original DataFrame:")
print(df)

def rep_char(str1):
    tchr = str1.group(0)
    if len(tchr) > 1:
        return tchr[0:1] # can change the value here on repetition

def unique_char(rep, sent_text):
    convert = re.sub(r'(w)1+', rep, sent_text) 
    return convert

df['normal_text']=df['text_lang'].apply(lambda x : unique_char(rep_char,x))

print("nRemove repetitive characters:")
print(df)

Sample Output:

Original DataFrame:
  text_code                    text_lang
0    t0001.      She livedd a long life.
1     t0002     How oold is your father?
2     t0003        What is tthe problem?
3     t0004  TThhis desk is used by Tom.

Remove repetitive characters:
  text_code                    text_lang                normal_text
0    t0001.      She livedd a long life.     She lived a long life.
1     t0002     How oold is your father?    How old is your father?
2     t0003        What is tthe problem?       What is the problem?
3     t0004  TThhis desk is used by Tom.  This desk is used by Tom.

 

Pandas Example – Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame

Sign up to get end-to-end “Learn By Coding” example.


Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.