Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove leading zero's from Time column in Data frame pandas

I have a dataframe like this:

Time       
07:20:45        
05:19:41       
10:16:26         
10:54:10

I want to remove leading zeros from the Time column:

Time       
7:20:45        
5:19:41       
10:16:26         
10:54:10

The code I’m using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df1['Time'] = df1['Time'].apply(lambda x: x.lstrip('0'))

Error it’s throwing:

AttributeError: ‘datetime.time’ object has no attribute ‘lstrip’

>Solution :

What ever the reason is to do that, in this case you could cast the pandas object to a string, so stripping will be possible:

df1['Time'] = df1['Time'].astype('str').apply(lambda x: x.lstrip('0'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading