Key Error when Merging columns from 2 Data Frames

Question:

I am trying to create a new df with certain columns from 2 others:
The first called visas_df:
enter image description here

And the second called cpdf:
enter image description here

I only need the highlighted columns. But when I try this:

df_joined = pd.merge(cpdf,visas_df["visas"],on="date")

The error appearing is: KeyError: ‘date’

I imagine this is due to how I created cpdf. It was a "bad dataset’ so I did some fidgeting.Line 12 on the code snipped below might have something to do, but I am clueless…
enter image description here

I even renamed the date columns of both dfs as "date and checked that dtypes and number of rows are the same.

Any feedback would be much appreciated. Thanks!

Asked By: Czar19

||

Answers:

df[‘visas’] in merge function is not a dataframe and its not contain date column. İf you want to df as a dataframe, you have to use double square bracket [[]] like this:

df_joined = pd.merge(cpdf,visas_df[["date","visas"]],on="date")
Answered By: Clegane
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.