Home > Software engineering >  Remove leading zeros only in case of integers datatype
Remove leading zeros only in case of integers datatype

Time:02-22

I want to remove leading zeros only in case of integer values. I have tried the following:

df['CRITERIA_VALUE'].str.replace(r'^0 (?=[[0-9]])', '', regex=True)

However this does not work. Does anybody know how to achieve this?

CodePudding user response:

Converting my comment to answer so that solution is easy to find for future visitors.

You may be able to use:

df['CRITERIA_VALUE'].str.replace(r'^0 (?=[0-9] $)', '', regex=True)

RegEx Details:

  • ^0 : Match 1 zeroes at the start.
  • (?=[0-9] $): Lookahead to assert that we have 1 or more ASCII digits before end position.

RegEx Demo

CodePudding user response:

This probably works:

df['CRITERIA_VALUE'] = df['CRITERIA_VALUE'].str.apply(lambda x: int(x))
  • Related