将两个数据框与分层列合并

这是我第一次在 pandas 中使用多重索引,我需要一些帮助来合并两个具有分层列的数据帧。这是我的两个数据框:


col_index = pd.MultiIndex.from_product([['a', 'b', 'c'], ['w', 'x']])

df1 = pd.DataFrame(np.ones([4,6]),columns=col_index, index=range(4))


     a         b         c     

     w    x    w    x    w    x

0  1.0  1.0  1.0  1.0  1.0  1.0

1  1.0  1.0  1.0  1.0  1.0  1.0

2  1.0  1.0  1.0  1.0  1.0  1.0

3  1.0  1.0  1.0  1.0  1.0  1.0


df2 = pd.DataFrame(np.zeros([2,6]),columns=col_index, index=range(2))


     a         b         c     

     w    x    w    x    w    x

0  0.0  0.0  0.0  0.0  0.0  0.0

1  0.0  0.0  0.0  0.0  0.0  0.0

当我使用合并方法时,我得到以下结果:


pd.merge(df1,df2, how='left', suffixes=('', '_2'), left_index = True, right_index= True ))


     a         b         c       a_2       b_2       c_2     

     w    x    w    x    w    x    w    x    w    x    w    x

0  1.0  1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0

1  1.0  1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0

2  1.0  1.0  1.0  1.0  1.0  1.0  NaN  NaN  NaN  NaN  NaN  NaN

3  1.0  1.0  1.0  1.0  1.0  1.0  NaN  NaN  NaN  NaN  NaN  NaN

但我想在较低级别上合并两个数据帧,后缀对 ['w', 'x'] 生效,如下所示:


     a                   b                   c               

     w  w_2    x  x_2    w  w_2    x  x_2    w  w_2    x  x_2

0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0

1  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0

2  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN

3  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN


拉莫斯之舞
浏览 108回答 1
1回答

忽然笑

您可以将joinormerge与swaplevel()or 一起使用reorder_levels。然后使用.sort_index()和 传递axis=1来按索引列排序。.join()当您像这样对索引进行合并时会更好。.swaplevel()当有两个级别时更好(如本例),而当.reorder_levels()有 3 个或更多级别时更好。以下是这些方法的 4 种组合。对于这个具体的例子,我认为.join()/.swaplevel()是最简单的(参见最后一个例子):df3 = (df1.reorder_levels([1,0],axis=1)       .join(df2.reorder_levels([1,0],axis=1), rsuffix='_2')       .reorder_levels([1,0],axis=1).sort_index(axis=1, level=[0, 1]))df3Out[1]:      a                   b                   c                    w  w_2    x  x_2    w  w_2    x  x_2    w  w_2    x  x_20  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.01  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.02  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN3  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaNdf3 = (pd.merge(df1.reorder_levels([1,0],axis=1),                df2.reorder_levels([1,0],axis=1),                how='left', left_index=True, right_index=True, suffixes = ('', '_2'))                .reorder_levels([1,0],axis=1).sort_index(axis=1, level=[0, 1]))df3Out[2]:      a                   b                   c                    w  w_2    x  x_2    w  w_2    x  x_2    w  w_2    x  x_20  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.01  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.02  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN3  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaNdf3 = (pd.merge(df1.swaplevel(axis=1),                df2.swaplevel(axis=1),                how='left', left_index=True, right_index=True, suffixes = ('', '_2'))                .swaplevel(axis=1).sort_index(axis=1, level=[0, 1]))df3Out[3]:      a                   b                   c                    w  w_2    x  x_2    w  w_2    x  x_2    w  w_2    x  x_20  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.01  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.02  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN3  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaNdf3 = (df1.swaplevel(i=0,j=1, axis=1)       .join(df2.swaplevel(axis=1), rsuffix='_2')       .swaplevel(axis=1).sort_index(axis=1, level=[0, 1]))df3Out[4]:      a                   b                   c                    w  w_2    x  x_2    w  w_2    x  x_2    w  w_2    x  x_20  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.01  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0  1.0  0.02  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN3  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN  1.0  NaN
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python