Home > Software engineering >  Key Error when Merging columns from 2 Data Frames
Key Error when Merging columns from 2 Data Frames

Time:10-20

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!

CodePudding user response:

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")
  • Related