9

Supposely I have dataframes as below:

Year Month Day
2003 1     8
2003 2     7

How to combine the Year, Month, and Day in the newly defined column in the dataframe as such the dataframe would be:

Year Month Day Date
2003 1     8   2003-1-8
2003 2     7   2003-2-7

Any idea on this?

I am using pandas python dataframe

Thanks!

Santiago Munez
  • 1,965
  • 5
  • 18
  • 16

2 Answers2

17
>>> from datetime import datetime
>>> df['Date'] = df.apply(lambda row: datetime(
                              row['Year'], row['Month'], row['Day']), axis=1)
>>> df
   Year  Month  Day                Date
0  2003      1    8 2003-01-08 00:00:00
1  2003      2    7 2003-02-07 00:00:00

Update 2020-03-12: The answer from sacul is better and faster:

%%timeit
df.apply(lambda row: datetime(
                              row['Year'], row['Month'], row['Day']), axis=1)

2.53 s ± 169 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# use below, above is slow!!!
%%timeit
pd.to_datetime(df[['Year','Month','Day']])

14.4 ms ± 3.37 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
ihightower
  • 3,093
  • 6
  • 34
  • 49
Viktor Kerkez
  • 45,070
  • 12
  • 104
  • 85
2

Better use pd.to_datetime:

df['Date'] = pd.to_datetime(df[['Year','Month','Day']])
>>> df
   Year  Month  Day       Date
0  2003      1    8 2003-01-08
1  2003      2    7 2003-02-07
sacuL
  • 49,704
  • 8
  • 81
  • 106