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.
CodePudding user response:
This probably works:
df['CRITERIA_VALUE'] = df['CRITERIA_VALUE'].str.apply(lambda x: int(x))