Tim
Tim

Reputation: 394

Pandas merge with inner join returns KeyError

I am trying to merge two Pandas dataframes using an inner join.

Dataframe A has this structure:

Date    datetime64[ns]
KR                  int64
dtype: object

Dataframe B has this structure:

Date        datetime64[ns]
US                   int64
Location            object
GeoId               object
dtype: object

My merge code is as follows:

C = pd.merge(A,B[['US']], on=['Date'], how='inner')

Jupyter Notebook returns the following error when I run the code:

KeyError: 'Date'

I've tried about 10 different ways and all of them return errors. Would appreciate some help to point out what is wrong.

Upvotes: 2

Views: 1312

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Try:

C = pd.merge(A, B[['Date', 'US']], on='Date')

With B[['US']], there is no 'Date' column in that view of the B dataframe.

Upvotes: 1

Related Questions