Combine date and time columns using datetime

2019-09-14 19:52发布

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:

   date        first_time   second_time .... 
0  2008/09/11    12:32        17:56
1  2016/12/02    06:43        14:02
2  2001/01/01    02:45        20:13
.
.
.

With .iterrows() I am able to break it down to each row. So row['date'] would be the date for that particular column. However, I need to use datetime to combine the date with each of the columns. I keep on getting errors for various methods I'm finding online. If I have row['date'] and row['first_time'], how could I combine them in the dataframe (also with date and every other time column)?

The end result should be this:

    first_datetime      second_datetime    .... 
0  2008/09/11 12:32     2008/09/11 17:56 
1  2016/12/02 06:43     2016/12/02 14:02 
2  2001/01/01 02:45     2001/01/01 20:13 
.
.
.

1条回答
干净又极端
2楼-- · 2019-09-14 20:44

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
查看更多
登录 后发表回答