1

I have a large pandas dataframe. One of my columns is a time column, and it currently looks like this 2014-01-01T00:52:00Z.

I want it to look like 1/1/14 00:55, but I have no idea how to make the dates look like this for an entire column of a dataframe?


Another example: 2014-10-24T08:55:00Z becomes 10/24/14 08:55.

325
  • 594
  • 8
  • 21

2 Answers2

2

Try making a for loop similar to the following. It should work well for you!

for item in file['Date_Time']:
        if item[5] != "0": #Oct, Nov, Dec
            newdat = item[5:7] + "/" + item[8:10] + "/" + item[2:4] + " " + item[11:16]
        else:
            newdat = item[6] + "/" + item[8:10] + "/" + item[2:4] + " " + item[11:16]
        file['New_Date_Time'] = newdat

Run this on the column you want to change. Cheers!

324
  • 702
  • 8
  • 28
1

Apply the following function to convert time format as you wish:


from datetime import datetime

def convert(date_string):
    format = "%Y-%m-%dT%H:%M:%S%z"
    date_struct = datetime.strptime(date_string, format)
    new_date_string = datetime.strftime(date_struct, "%m/%d/%Y %H:%M")
    return new_date_string

date_string = "2014-10-24T08:55:00Z"
print (convert(date_string))

And here is the result:

10/24/2014 08:55
Pouya Esmaeili
  • 1,265
  • 4
  • 11
  • 25