Combine Date And Time Columns Using Datetime
I am trying to combine the date to multiple time columns in my dataframe. I am able to iterate through each row, but I am confused as to how I combine the columns. For example:
Solution 1:
You can first set_index
with column date
and then in loop of time
columns convert to_datetime
:
df = df.set_index('date')
for col in df.columns:
df[col] = pd.to_datetime(df.index + df[col], format='%Y/%m/%d%H:%M')
#if necessary rename columns
df.columns = df.columns.str.replace('time','datetime')
df = df.reset_index(drop=True)
print (df)
first_datetime second_datetime
0 2008-09-11 12:32:00 2008-09-11 17:56:00
1 2016-12-02 06:43:00 2016-12-02 14:02:00
2 2001-01-01 02:45:00 2001-01-01 20:13:00
print (df.dtypes)
first_datetime datetime64[ns]
second_datetime datetime64[ns]
dtype: object
For more dynamic solution convert only columns with time
in name:
df = df.set_index('date')
#extract only time columns
cols = df.columns[df.columns.str.contains('time')]
for col in cols:
df[col] = pd.to_datetime(df.index + df[col], format='%Y/%m/%d%H:%M')
df.columns = df.columns.str.replace('time','datetime')
df = df.reset_index(drop=True)
print (df)
first_datetime second_datetime
0 2008-09-11 12:32:00 2008-09-11 17:56:00
1 2016-12-02 06:43:00 2016-12-02 14:02:00
2 2001-01-01 02:45:00 2001-01-01 20:13:00
Post a Comment for "Combine Date And Time Columns Using Datetime"