【发布时间】:2016-01-10 08:06:48
【问题描述】:
我有一个 multiIndex 数据帧,我正在与另一个 multiIndex 数据帧合并,它合并成功,但现在它只有一个索引?
climate_df2.index.names
Out[79]: FrozenList(['key', 'Y'])
fileformat_df.index.names
Out[80]: FrozenList(['key', 'Y'])
climate_df3 = pd.merge(climate_df2.reset_index(), fileformat_df.reset_index(),
on = ["key","Y"], how = "inner").set_index("key","Y")
climate_df3.index.names
Out[81]: FrozenList(['key'])
climate_df3.columns
Out[82]: Index(['Y', 'DatasetName', 'Organization', 'URL', 'FileFormat'], dtype='object')
但现在以前的索引“Y”是合并数据框中的一列。所以我要么想让合并的 DF 首先返回两个索引,要么只是将“Y”列添加到合并的数据框中。
【问题讨论】:
标签: python pandas indexing merge multi-index