1

I am trying to remove the '0' leading my data

My dataframe looks like this

Id    Year   Month    Day
1     2019   01       15
2     2019   03       30
3     2019   10       20
4     2019   11       18

Note: 'Year','Month','Day' columns data types are object

I get the 'Year','Month','Day' columns by extracting it from a date. I want to remove the '0' at the beginning of each months.

Desired Ouput:

Id    Year   Month    Day
1     2019   1        15
2     2019   3        30
3     2019   10       20
4     2019   11       18

What I tried to do so far: df['Month'].str.lstrip('0')

But it did not work.

Any solution? Thank you!

Killi Mandjaro
  • 145
  • 2
  • 15

1 Answers1

1

You could use re package and apply regex on it

import re
# Create sample data
d = pd.DataFrame(data={"Month":["01","02","03","10","11"]})
d["Month" = d["Month"].apply(lambda x: re.sub(r"^0+", "", x))

Result:

0     1
1     2
2     3
3    10
4    11
Name: Month, dtype: object

If you are 100% that Month column will always contain numbers, then you could simply do:

d["Month"] = d["Month"].astype(int)
Capie
  • 976
  • 1
  • 8
  • 20