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:
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…
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!
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")
I am trying to create a new df with certain columns from 2 others:
The first called visas_df:
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…
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!
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")