1
votes

How can I merge two pandas dataframes with different lengths like those:

df1 = Index  block_id   Ut_rec_0
       0       0           7
       1       1           10
       2       2           2
       3       3           0
       4       4           10
       5       5           3
       6       6           6
       7       7           9


df2 = Index  block_id   Ut_rec_1
       0       0           3
       2       2           5
       3       3           5
       5       5           9
       7       7           4

result = Index  block_id    Ut_rec_0    Ut_rec_1
           0       0           7           3
           1       1           10          NaN
           2       2           2           5
           3       3           0           5
           4       4           10          NaN
           5       5           3           9
           6       6           6           NaN
           7       7           9           4

I already tried something like, but it did not work:

df_result = pd.concat([df1, df2], join_axes=[df1['block_id']])

I already tried: df_result = pd.concat([df1,df2,axis = 1) But the result was:

Index   block_id    Ut_rec_0    Index   block_id    Ut_rec_1
  0            0          7         0.0   0.0         3.0
  1            1          10        1.0   2.0         5.0
  2            2          2         2.0   3.0         5.0
  3            3          0         3.0   5.0         9.0
  4            4          10        4.0   7.0         4.0
  5            5          3         NaN   NaN         NaN
  6            6          6         NaN   NaN         NaN
  7            7          9         NaN   NaN         NaN
3
pd.concat([df1, df2],axis=1) - BENY
did not give the expected result - Matheus Damasceno
I refaced the question with the output using pd.concat([df1, df2],axis=1) output - Matheus Damasceno

3 Answers

1
votes

pandas.DataFrame.join can "join" dataframes based on overlap in column data (or index). Something like this will likely work for you:

df1.join(df2.set_index('block_id'), on='block_id')
0
votes

As @Wen said best would be using concat with axis as 1, like the below code:

pd.concat([df1, df2],axis=1)
0
votes

you need, pd.merge with outer join,

pd.merge(df1,df2,on=['Index','block_id'],how='outer')
#[out]
#Index  block_id    Ut_rec_0    Ut_rec_1
#0      0               7       3.0
#1      1               10      NaN
#2      2               2       5.0
#3      3               0       5.0
#4      4               10      NaN
#5      5               3       9.0
#6      6               6       NaN
#7      7               9       4.0