Python & Pandas: merge columns into date

In my dataframe time will be divided into 3 columns year , month , day , like this enter image description here

How can I convert them to date , so I can do seris time analysis?

I can do it:

 df.apply(lambda x:'%s %s %s' % (x['year'],x['month'], x['day']),axis=1) 

which outputs

 1095 1954 1 1 1096 1954 1 2 1097 1954 1 3 1098 1954 1 4 1099 1954 1 5 1100 1954 1 6 1101 1954 1 7 1102 1954 1 8 1103 1954 1 9 1104 1954 1 10 1105 1954 1 11 1106 1954 1 12 1107 1954 1 13 

But what follows?

UPDATE: Here is what I get:

 from datetime import datetime df['date']= df.apply(lambda x:datetime.strptime("{0} {1} {2}".format(x['year'],x['month'], x['day']), "%Y %m %d"),axis=1) df.index= df['date'] 
+8
python pandas
source share
1 answer

Here's how to convert the value to time:

 import datetime df.apply(lambda x:datetime.strptime("{0} {1} {2} 00:00:00".format(x['year'],x['month'], x['day']), "%Y %m %d %H:%M:%S"),axis=1) 
+8
source share

All Articles