<tfoot id='j0iW9'></tfoot>

    <small id='j0iW9'></small><noframes id='j0iW9'>

    • <bdo id='j0iW9'></bdo><ul id='j0iW9'></ul>
    <legend id='j0iW9'><style id='j0iW9'><dir id='j0iW9'><q id='j0iW9'></q></dir></style></legend>

      1. <i id='j0iW9'><tr id='j0iW9'><dt id='j0iW9'><q id='j0iW9'><span id='j0iW9'><b id='j0iW9'><form id='j0iW9'><ins id='j0iW9'></ins><ul id='j0iW9'></ul><sub id='j0iW9'></sub></form><legend id='j0iW9'></legend><bdo id='j0iW9'><pre id='j0iW9'><center id='j0iW9'></center></pre></bdo></b><th id='j0iW9'></th></span></q></dt></tr></i><div id='j0iW9'><tfoot id='j0iW9'></tfoot><dl id='j0iW9'><fieldset id='j0iW9'></fieldset></dl></div>

        合并具有多索引的两个数据帧

        时间:2024-08-22

          <legend id='Nyt8A'><style id='Nyt8A'><dir id='Nyt8A'><q id='Nyt8A'></q></dir></style></legend>
          <tfoot id='Nyt8A'></tfoot>
        1. <i id='Nyt8A'><tr id='Nyt8A'><dt id='Nyt8A'><q id='Nyt8A'><span id='Nyt8A'><b id='Nyt8A'><form id='Nyt8A'><ins id='Nyt8A'></ins><ul id='Nyt8A'></ul><sub id='Nyt8A'></sub></form><legend id='Nyt8A'></legend><bdo id='Nyt8A'><pre id='Nyt8A'><center id='Nyt8A'></center></pre></bdo></b><th id='Nyt8A'></th></span></q></dt></tr></i><div id='Nyt8A'><tfoot id='Nyt8A'></tfoot><dl id='Nyt8A'><fieldset id='Nyt8A'></fieldset></dl></div>
                <tbody id='Nyt8A'></tbody>

                <small id='Nyt8A'></small><noframes id='Nyt8A'>

                • <bdo id='Nyt8A'></bdo><ul id='Nyt8A'></ul>

                  本文介绍了合并具有多索引的两个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经看过几篇关于这个问题的帖子,但是我不能理解Merge、Join和Concat将如何处理这个问题。如何合并两个数据帧以查找匹配的索引?

                  在:

                  import pandas as pd
                  import numpy as np
                  row_x1 = ['a1','b1','c1']
                  row_x2 = ['a2','b2','c2']
                  row_x3 = ['a3','b3','c3']
                  row_x4 = ['a4','b4','c4']
                  index_arrays = [np.array(['first', 'first', 'second', 'second']), np.array(['one','two','one','two'])]
                  df1 = pd.DataFrame([row_x1,row_x2,row_x3,row_x4], columns=list('ABC'), index=index_arrays)
                  print(df1)
                  

                  输出:

                               A   B   C
                  first  one  a1  b1  c1
                         two  a2  b2  c2
                  second one  a3  b3  c3
                         two  a4  b4  c4
                  

                  在:

                  row_y1 = ['d1','e1','f1']
                  row_y2 = ['d2','e2','f2']
                  df2 = pd.DataFrame([row_y1,row_y2], columns=list('DEF'), index=['first','second'])
                  print(df2)
                  

                  输出

                           D   E   F
                  first   d1  e1  f1
                  second  d2  e2  f2
                  

                  换句话说,如何将它们合并以实现DF3(如下所示)?

                  row_x1 = ['a1','b1','c1']
                  row_x2 = ['a2','b2','c2']
                  row_x3 = ['a3','b3','c3']
                  row_x4 = ['a4','b4','c4']
                  row_y1 = ['d1','e1','f1']
                  row_y2 = ['d2','e2','f2']
                  
                  row_z1 = row_x1 + row_y1
                  row_z2 = row_x2 + row_y1
                  row_z3 = row_x3 + row_y2
                  row_z4 = row_x4 + row_y2
                  
                  df3 = pd.DataFrame([row_z1,row_z2,row_z3,row_z4], columns=list('ABCDEF'), index=index_arrays)
                  print(df3)
                  

                  输出

                               A   B   C   D   E   F
                  first  one  a1  b1  c1  d1  e1  f1
                         two  a2  b2  c2  d1  e1  f1
                  second one  a3  b3  c3  d2  e2  f2
                         two  a4  b4  c4  d2  e2  f2
                  

                  推荐答案

                  选项1
                  使用pd.DataFrame.reindex+pd.DataFrame.join
                  reindex有一个方便的level参数,允许您在不存在的索引级别上展开。

                  df1.join(df2.reindex(df1.index, level=0))
                  
                               A   B   C   D   E   F
                  first  one  a1  b1  c1  d1  e1  f1
                         two  a2  b2  c2  d1  e1  f1
                  second one  a3  b3  c3  d2  e2  f2
                         two  a4  b4  c4  d2  e2  f2
                  

                  选项2
                  您可以重命名您的轴,join将起作用

                  df1.rename_axis(['a', 'b']).join(df2.rename_axis('a'))
                  
                               A   B   C   D   E   F
                  a      b                          
                  first  one  a1  b1  c1  d1  e1  f1
                         two  a2  b2  c2  d1  e1  f1
                  second one  a3  b3  c3  d2  e2  f2
                         two  a4  b4  c4  d2  e2  f2
                  

                  您可以继续rename_axis以获得所需的结果

                  df1.rename_axis(['a', 'b']).join(df2.rename_axis('a')).rename_axis([None, None])
                  
                               A   B   C   D   E   F
                  first  one  a1  b1  c1  d1  e1  f1
                         two  a2  b2  c2  d1  e1  f1
                  second one  a3  b3  c3  d2  e2  f2
                         two  a4  b4  c4  d2  e2  f2
                  

                  这篇关于合并具有多索引的两个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:不协调命令机器人没有响应(Python) 下一篇:如何在Pandas中连接包含List(Series)的两列

                  相关文章

                  <tfoot id='rwhx8'></tfoot>

                • <legend id='rwhx8'><style id='rwhx8'><dir id='rwhx8'><q id='rwhx8'></q></dir></style></legend>
                  <i id='rwhx8'><tr id='rwhx8'><dt id='rwhx8'><q id='rwhx8'><span id='rwhx8'><b id='rwhx8'><form id='rwhx8'><ins id='rwhx8'></ins><ul id='rwhx8'></ul><sub id='rwhx8'></sub></form><legend id='rwhx8'></legend><bdo id='rwhx8'><pre id='rwhx8'><center id='rwhx8'></center></pre></bdo></b><th id='rwhx8'></th></span></q></dt></tr></i><div id='rwhx8'><tfoot id='rwhx8'></tfoot><dl id='rwhx8'><fieldset id='rwhx8'></fieldset></dl></div>

                  <small id='rwhx8'></small><noframes id='rwhx8'>

                      <bdo id='rwhx8'></bdo><ul id='rwhx8'></ul>