使用datetime合并日期和时间列 [英] 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:
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
.
.
.
使用.iterrows(),我可以将其分解为每一行.因此row ['date']将是该特定列的日期.但是,我需要使用datetime将日期与每个列组合在一起.我不断在网上找到各种方法而出错.如果我有row ['date']和row ['first_time'],如何将它们合并到数据框中(以及日期和其他每个时间列)?
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)?
最终结果应该是这样:
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
.
.
.
推荐答案
您可以首先 to_datetime
:
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
要获得更多动态解决方案,请仅转换名称为time
的列:
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
这篇关于使用datetime合并日期和时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!