Reputation: 55
I want to remove leading zeros only in case of integer values. I have tried the following:
df['col'].str.replace(r'^0+(?=[[0-9]])', '', regex=True)
However this does not work. Does anybody know how to achieve this?
Upvotes: 1
Views: 378
Reputation: 785156
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.Upvotes: 3
Reputation: 2812
This probably works:
df['CRITERIA_VALUE'] = df['CRITERIA_VALUE'].str.apply(lambda x: int(x))
Upvotes: 0