PythonBeginner
PythonBeginner

Reputation: 483

Pandas KeyError: when merging 2 dataframes

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')

Upvotes: 1

Views: 3885

Answers (1)

Paul
Paul

Reputation: 1897

Try:

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

Upvotes: 6

Related Questions