freddy_or_json
freddy_or_json

Reputation: 55

Remove leading zeros only in case of integers datatype

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

Answers (2)

anubhava
anubhava

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.

RegEx Demo

Upvotes: 3

Sadra
Sadra

Reputation: 2812

This probably works:

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

Upvotes: 0

Related Questions