Fluxy
Fluxy

Reputation: 2978

How to convert time string field into "hh:mm" format?

I have pandas DataFrame like this:

import pandas as pd
d = {
         'time': [0, 100, 200, 1400, 1500],
         'value': [9, 8, 7, 6, 5],
         'three': ['a', 'b', 'a', 'a', 'b']
    }
df = pd.DataFrame(d)

How want to convert time into hh:mm format to get: 00:00, 01:00, 02:00, etc.

How can I do it without if-then?

Upvotes: 0

Views: 45

Answers (1)

Sathiya Sarathi
Sathiya Sarathi

Reputation: 439

You need to convert it to string and perform a zfill() and concatenate using':'

d['time'] = [str(a).zfill(4)[:2]+':'+str(a).zfill(4)[2:4] for a in d['time']]

Upvotes: 2

Related Questions