我正在进行不同的查询,我从中获得3个不同的列(date_1,date_2,date_3)。我需要在一个Dataframe中使用它们,所以我使用concat函数。我得到了这个:
resultado = pd.concat([date_1, date_2, date_3], axis=1)
date_1 date_2 date_3
01/01/2009 0:00 01/01/2009 00:00 01/01/2009 0:00
01/01/2009 0:10 01/01/2009 00:10 01/01/2009 0:10
01/01/2009 0:20 01/01/2009 00:20 01/01/2009 0:30
01/01/2009 0:30 01/01/2009 00:30 01/01/2009 0:40
01/01/2009 0:40 01/01/2009 00:50 01/01/2009 0:50
01/01/2009 0:50 NaT NaT
但我想要/需要的是这样的。根据索引
添加列值 date_1 date_2 date_3
01/01/2009 0:00 01/01/2009 00:000 01/01/2009 0:00
01/01/2009 0:10 01/01/2009 00:10 01/01/2009 0:10
01/01/2009 0:20 01/01/2009 00:20 NaT
01/01/2009 0:30 01/01/2009 00:30 01/01/2009 0:30
01/01/2009 0:40 NaT 01/01/2009 0:40
01/01/2009 0:50 01/01/2009 00:50 01/01/2009 0:50
答案 0 :(得分:0)
您可以按DatetimeIndex
:
#get all columns in list
sers = [date_1['date_1'], date_2['date_2'], date_3['date_3']]
#convert data to datetime
sers = [pd.to_datetime(x) for x in sers]
#create DatetimeIndex for align Series, remove NaNs rows and last remove Index
resultado = pd.concat([x.to_frame().set_index(x) for x in sers], axis=1)
resultado = resultado.dropna(how='all').reset_index(drop=True)
print (resultado)
date_1 date_2 date_3
0 2009-01-01 00:00:00 2009-01-01 00:00:00 2009-01-01 00:00:00
1 2009-01-01 00:10:00 2009-01-01 00:10:00 2009-01-01 00:10:00
2 2009-01-01 00:20:00 2009-01-01 00:20:00 NaT
3 2009-01-01 00:30:00 2009-01-01 00:30:00 2009-01-01 00:30:00
4 2009-01-01 00:40:00 NaT 2009-01-01 00:40:00
5 2009-01-01 00:50:00 2009-01-01 00:50:00 2009-01-01 00:50:00