Reputation: 101
I have a dataframe as below:
Year | Month | Day | Hour |
---|---|---|---|
2022 | 8 | 21 | 5 |
2022 | 8 | 21 | 6 |
I want the above df as follows
Date | Time |
---|---|
21-08-2022 | 05:00:00 |
22-08-2022 | 06:00:00 |
Can anyone please tell me how to do it using python?
Upvotes: 0
Views: 18
Reputation: 862511
Because columns names are exactly ['Year', 'Month', 'Day', 'Hour']
you can use to_datetime
and then Series.dt.date
,
Series.dt.time
:
s = pd.to_datetime(df[['Year', 'Month', 'Day', 'Hour']])
#if no another columns
#s = pd.to_datetime(df)
df1 = pd.DataFrame({'Date': s.dt.date, 'Time': s.dt.time})
print (df1)
Date Time
0 2022-08-21 05:00:00
1 2022-08-21 06:00:00
Upvotes: 1