1

I want to merge two dataframes based on equal column values. The problem is that one of my columns have duplicated row values, which cannot be drop since it's correlated to another columns. Here's an example of my two dataframes:

DF1 DF2

Essentialy, I want to merge this two dataframes based on equal values of FromPatchID (df1) and Id (df2) columns, in order to get something like this:

FromPatchID ToPatchID ... Id MMM LB 1 1 ... 1 26.67 27.67 1 2 ... 1 26.67 27.67 1 3 ... 1 26.67 27.67 2 1 ... 2 26.50 27.50 3 1 ... 3 26.63 27.63 

I already tried a simple merge with df_merged = pd.merge(df1, df2, on=['FromPatchID','Id']), but I got KeyError indicating to check for duplicates in FromPatchID column.

1 Answer 1

2

You have to specify the different column names to match on with left_on and right_on. Also specify how='right' to use only keys from the right frame.

df_merged = pd.merge(df1, df2, left_on='FromPatchID', right_on='Id', how='right') 
Sign up to request clarification or add additional context in comments.

1 Comment

It worked, thank you! The only point is that I had to specify how='left' instead of 'right' to merge within the whole range of my df1 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.