Home > Net >  How to remove unwanted dots from strings in pandas column?
How to remove unwanted dots from strings in pandas column?

Time:06-20

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:

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?

CodePudding user response:

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
  • Related