I have a column named timestamp and I would like to rename the column in a more appropriate format. Eg- 353 as 3:53 pm. How can I do this using pandas or appropriate string manipulation?
Code:
c=pd.DataFrame({"Timestamp":x,"Latitude":y,"Longitude":z})
c.head()
Need timestamp converted to format from 303- 3:03pm (The timestamp is string type)
CodePudding user response:
You can call apply
on the column and pass a function that will split each string and insert a colon:
c['Timestamp'].apply(lambda x: x[0:-2] ':' x[-2:])
CodePudding user response:
This will also work:
from datetime import datetime
c['Timestamp'].apply(lambda x: datetime.strptime(x.rjust(4, '0'), '%H%M').strftime('%H:%M'))