Advait
Advait

Reputation: 74

Merging of data columns using pandas

I am trying to merge tables based on a key that is common to them using pandas and I am constantly getting the error :

KeyError: 'Host Key of Allocated Locations'

Table one has data: highlighted value is the 'Host Key of Allocated Locations'. Also, it has duplicate values which I want to have as they will be needed in further analysis. enter image description here

Table 2: highlighted value is the PK key enter image description here

df3 = pd.merge(timetable_2020_df, joined_uom_space_df, on='Host Key of Allocated Locations', how='left')

I tried but I cannot understand what the issue is.

duplicate data is :

enter image description here

Upvotes: 0

Views: 38

Answers (1)

ac24
ac24

Reputation: 5565

The on keyword only works when you have the same named column in both dataframes. Your right dataframe does not have the 'Host...' column, hence a keyerror is being raised.

You will need to use left_on and right_on keywords to specify the joining columns in your left and right dataframes.

Upvotes: 1

Related Questions