如果索引和长度都不匹配,如何合并两个数据帧?

时间:2016-08-23 16:48:12

标签: python pandas

我有两个数据帧predictor_df和solution_df,如下所示:

predictor_df 

1000 A B C.   1001 1 2 3   1002 4 5 6   1003 7 8 9   1004 Nan Nan Nan

and a solution_df

0 D    1 10    2 11    3 12

名称的原因是predictor_df用于对其列进行一些分析以得到analysis_df。我的分析将行中的Nan值留在predictor_df中,因此更短 solution_df

现在我想知道如何加入这两个数据帧以获得我的最终数据框

  A      B    C    D
  1      2    3   10
  4      5    6   11
  7      8    9   12
 Nan    Nan  Nan

请引导我完成它。提前致谢。 编辑:我试图合并两个数据帧,但结果是这样的,

      A      B    C    D
      1      2    3   Nan
      4      5    6   Nan
      7      8    9   Nan
     Nan    Nan  Nan

编辑2:当我做pd.concat([predictor_df, solution_df], axis = 1)时 它就像这样

          A         B      C   D
          Nan      Nan   Nan  10
          Nan      Nan   Nan  11
          Nan      Nan   Nan  12
          Nan      Nan   Nan  Nan

1 个答案:

答案 0 :(得分:1)

您可以将reset_indexdrop=True一起使用,将索引重置为默认整数索引。

pd.concat([df_1.reset_index(drop=True), df_2.reset_index(drop=True)], axis=1)

     A    B    C     D
0    1    2    3  10.0
1    4    5    6  11.0
2    7    8    9  12.0
3  Nan  Nan  Nan   NaN
相关问题