我有以下数据框:
df1 = pd.DataFrame.from_dict({'A':[3,5,1,7], 'DateTime' : pd.date_range("11:00", "14:00", freq="60min")}).set_index('DateTime')
df2 = pd.DataFrame.from_dict({'B':[13,15,1,17], 'DateTime' : pd.date_range("12:00", "15:00", freq="60min")}).set_index('DateTime')
我正在尝试以下方法:
pd.concat([df1, df2], join='outer')
A B
DateTime
2017-04-19 11:00:00 3.0 NaN
2017-04-19 12:00:00 5.0 NaN
2017-04-19 13:00:00 1.0 NaN
2017-04-19 14:00:00 7.0 NaN
2017-04-19 12:00:00 NaN 13.0
2017-04-19 13:00:00 NaN 15.0
2017-04-19 14:00:00 NaN 1.0
2017-04-19 15:00:00 NaN 17.0
如何获得以下内容:
A B
DateTime
2017-04-19 11:00:00 3.0 NaN
2017-04-19 12:00:00 5.0 13.0
2017-04-19 13:00:00 1.0 15.0
2017-04-19 14:00:00 7.0 1.0
2017-04-19 15:00:00 NaN 17.0
答案 0 :(得分:7)
您需要将轴设置为1.
pd.concat([df1, df2], join='outer', axis=1)
A B
DateTime
2017-04-19 11:00:00 3.0 NaN
2017-04-19 12:00:00 5.0 13.0
2017-04-19 13:00:00 1.0 15.0
2017-04-19 14:00:00 7.0 1.0
2017-04-19 15:00:00 NaN 17.0