1

I am trying to merge 2 columns from DF2 to DF1 (Note DF2 has multiple columns). I am merging these columns on "Unique ID".

When I do so I get the following error KeyError: 'Unique ID'

I have read online that the KeyError error can be caused by indexing issues, and not using the exact column names. However I have reset the index and I have checked and double checked that the column we're merging on ("Unique ID") is in both DF1 and DF2.

I would rather not share the DFs as they contain sensitive information

This is the code I am using

df = pd.merge(DF1,DF2[['System','Platform']],on='Unique ID', how='left')

2
  • 2
    you are only using the columns 'Segment' and 'Platform' from the DF2? So it cannot find 'Unique ID' in the second dataframe. Commented Dec 1, 2021 at 10:07
  • @Paul I have updated the question. I am merging the system and platform column for DF2 to DF1 Commented Dec 1, 2021 at 10:09

1 Answer 1

6

Try:

df = pd.merge(DF1,DF2[['System','Platform', 'Unique ID']],on='Unique ID', how='left')

Sign up to request clarification or add additional context in comments.

2 Comments

why do you need to specify the first unique ID
The column you are merging on should be in both dataframes. DF2[['System','Platform']] returns a dataframe without 'Unique ID', so it cannot match that column with the Unique ID column in DF1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.