Home > Software design >  Merging two dataframes that have only one column that names the same
Merging two dataframes that have only one column that names the same

Time:09-29

I am new at merging dataframes. I want to compare two dataframes:

left:

    A
0   a
1   b
2   d

right

    A
0   a
1   b
2   c
3   d

I want something like this:

    A   A_1
0   a   a
1   b   b
2   NaN c
3   d   d

Is it possible to do this using merge/join/compare functions?

CodePudding user response:

Yes this is possible. What you are asking for is a right outer join/merge. See this detailed overview.

df_left = pd.DataFrame({'A': ['a', 'b', 'd']})
df_right = pd.DataFrame({'A': ['a', 'b', 'c', 'd']})

df_left.merge(df_right.rename(columns={'A':'A_1'}), left_on='A', right_on='A_1', how='right')

Out

     A A_1
0    a   a
1    b   b
2  NaN   c
3    d   d

CodePudding user response:

If you only want to get the info about the merge you can use indicator argument of pd.merge:

pd.merge(df1, df2, how='outer', on='A', indicator=True)

   A      _merge
0  a        both
1  b        both
2  d        both
3  c  right_only

CodePudding user response:

You can also use join:

>>> left.set_index('A', drop=False) \
        .join(right.set_index('A', drop=False), rsuffix='_1', how='outer') \
        .reset_index(drop=True)

     A A_1
0    a   a
1    b   b
2  NaN   c
3    d   d
  • Related