Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove unwanted dots from strings in pandas column?

I have a data that contains the column as following:

mouse.pad.v.1.2
key.board.1.0.c30
pen.color.4.32.r

I am removing digits by

df["parts"]= df["parts"].str.replace('\d+', '')

Once the digits are removed the data looks like the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

mouse.pad.v..
key.board...c
pen.color...r

what I want to do is to replace more than one dot from the column with just one dot. Ideal output should be

mouse.pad.v
key.board.c
pen.color.r

I tried using

df["parts"]= df["parts"].str.replace('..', '.')

But I am not sure how many dots will be combined together. Is there a way to automate it?

>Solution :

Try:

df["parts"] = df["parts"].str.replace(r"\.*\d+", "", regex=True)
print(df)

Prints:

         parts
0  mouse.pad.v
1  key.board.c
2  pen.color.r

Input dataframe:

               parts
0    mouse.pad.v.1.2
1  key.board.1.0.c30
2   pen.color.4.32.r
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading