-1

I need to convert the date format of my csv file into the proper pandas format so I could sort it later on. My current format cannot be interacted reasonably in pandas so I had to convert it.

This is what my csv file looks like:

ARTIST,ALBUM,TRACK,DATE
ARTIST1,ALBUM1,TRACK1,23 Nov 2019 02:08
ARTIST1,ALBUM1,TRACK1,23 Nov 2019 02:11
ARTIST1,ALBUM1,TRACK1,23 Nov 2019 02:15

So far I've successfully converted it into pandas format by doing this:

df= pd.read_csv("mycsv.csv", delimiter=',')
convertdate= pd.to_datetime(df["DATE"])
print convertdate

####

#Original date format: 23 Nov 2019 02:08
#Output and desired date format: 2019-11-23 02:08:00

However, that only changes the values in the entire "DATE" column. Printing the dataframe of the csv file still outputs the original, non-converted date format. I need to append the converted format into the source csv file.

My desired output would then be

ARTIST,ALBUM,TRACK,DATE
ARTIST1,ALBUM1,TRACK1,2019-11-23 02:08:00
ARTIST1,ALBUM1,TRACK1,2019-11-23 02:11:00
ARTIST1,ALBUM1,TRACK1,2019-11-23 02:15:00
Tyler Joseph
  • 353
  • 1
  • 5
  • 13

2 Answers2

1

There are many options to the read_csv method. Make sure to read the data in in the format you want instead of fixing it later.

df = pd.read_csv('mycsv.csv"', parse_dates=['DATE'])

Just pass in to the parse_dates argument the column names you want transformed.

There were 2 problems in the original code. It wasn't a part of the original dataframe because you didn't save it back to the column once you transformed it.

so instead of:

convertdate= pd.to_datetime(df["DATE"])

use:

df["DATE"]= pd.to_datetime(df["DATE"])

and for goodness sake stop using python 2.

Back2Basics
  • 7,406
  • 2
  • 32
  • 45
0
dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')

df = pd.read_csv('mycsv.csv', parse_dates=['DATE'], date_parser=dateparse)