Home > database >  Remove zero after first space pandas regex
Remove zero after first space pandas regex

Time:10-14

I have a dataframe like this:

df = 

number
 123 1234
 123 0123
 123 123 01234
 123 0123 0023

I want to remove only 0 only after first space as a new column. Desired output:

number            filtered
 123 1234          123 1234
 123 0123          123 123
 123 123 01234     123 123 1234
 123 0123 0023     123 123 0023

My try is:

df['filtered'] = df['number'].replace(r'\s(.)', '', regex=True)

But I realized that it is removing first character after space not only zero


I am ok even with different approach, not regex only

CodePudding user response:

You can use this regex to match from beginning of line up to a zero 0 after the first space, and then replace the match with capture group 1:

^(\S*\s)0

Regex demo on regex101

In python:

df['filtered'] = df['number'].replace(r'^(\S*\s)0', r'\1', regex=True)

Output:

     123 1234
      123 123
 123 123 1234
 123 123 0023

CodePudding user response:

Try this

\s0

Python

df['filtered'] = df['number'].replace(r'\s0', ' ', regex=True)
  • Related