【发布时间】:2018-07-22 03:06:19
【问题描述】:
假设我有如下两个数据框:
import pandas as pd
index = pd.MultiIndex.from_tuples([('one', '1993-02-02'), ('one', '1994-02-03'), ('two', '1995-02-18'), ('two', '1996-03-01')])
s = pd.DataFrame(np.arange(1.0, 5.0), index=index)
s.rename(columns = {0 : 'test1'}, inplace = True)
s.index.set_names(['name','date'], in place=True)
index = pd.MultiIndex.from_tuples([('one', '19930630'), ('one', '19940630'), ('two', '19950630'), ('two', '19960630')])
d = pd.DataFrame(np.arange(1.0, 5.0), index=index)
d.rename(columns = {0 : 'test2'}, inplace = True)
d.index.set_names(['name','date'], in place=True)
其中s和d如下:
我想根据年份索引合并它们,使它们如下所示:
我将不胜感激任何帮助。
【问题讨论】:
-
你到底想合并什么?日期的年份部分?
-
@philngo 是的,我进行了编辑以给索引命名
标签: pandas dataframe concat multi-index