Merge two data frames in Python on column with non-unique values

Viewed 11438

I am trying to merge two dataframes in Python based on column 'X'.

The column X in left dataframe has non-unique values and the column X in right dataframe has unique values. How can I merge the values from the right dataframe into the left dataframe?

I want to merge rows from df2 into df1 to form df3

df1 = pd.DataFrame({'A': ['NA','EU','LA','ME'],
                    'B': [50, 23,21,100],
                    'X': ['IW233', 'IW455', 'IW455', 'IW100']})

df2 = pd.DataFrame({'C': [50, 12, 12, 11, 10, 16],
                    'X': ['IW455', 'IW200', 'IW233', 'IW150', 'IW175', 'IW100'],
                    'D': ['Aug', 'Sep', 'Jan', 'Feb', 'Dec', 'Nov']})

df3:1

2 Answers
Related