Search code examples
pythonpandasdataframemergeconcatenation

How can these two dataframes be merged on a specific key?


I have two dataframes, both with a column 'hotelCode' that is type string. I made sure to convert both columns to string beforehand.

The first dataframe, we'll call old_DF looks like so:

enter image description here

and the second dataframe new_DF looks like:

enter image description here

I have been trying to merge these unsuccessfully. I've tried

final_DF = new_DF.join(old_DF, on = 'hotelCode')

and get this error:

enter image description here

I've tried a variety of things: changing the index name, various merge/join/concat and just haven't been successful.

Ideally, I will have a new dataframe where you have columns [[hotelCode, oldDate, newDate]] under one roof.


Solution

  • import pandas as pd
    
    final_DF = pd.merge(old_DF, new_DF, on='hotelCode', how='outer')