0

I am merging two data frames, but when I try to only include two columns I get an error.

df_cluster_centers.merge(df_inventory_location_info[['Total Cost']],
                         right_on=['Latitude','Longitude'],
                         left_on = ['Cluster Latitude','Cluster Longitude'],
                         how = 'left')

KeyError: 'Latitude'

When I take out [['Total Cost']] it merges fine, but then I have to drop all the other columns. I just want to know why it doesn't work.

  • Please share your data. Without it, it is not possible to help you out – some_programmer Feb 01 '20 at 14:35
  • firstly, when you call `df_inventory_location_info[['Total Cost']]` you are returning a new data frame with only that column and the index. secondly your column may have trailing white space. The new data frame is your issue here. change it to `df_inventory_location_info[['Total Cost','Latitude','Longitude']]` – Umar.H Feb 01 '20 at 14:42
  • Thanks Datanovice. As soon as I read '...you are returning a new date frame...', the light bulb came on. I had to include the columns that I am joining in the new data frame. This is why it was giving me the error 'Latitude', it wasn't included in the new data frame. I changed the code to include 'Latitude' and 'Longitude' and it worked. Still had to drop the keys, but it is much better. Thanks. – Troy Mills Feb 01 '20 at 14:58
  • no problemo, I'd close this as a duplicate as it's covered in the merging 101 post and we want to direct people to that post for a thorought intro into merging – Umar.H Feb 01 '20 at 15:08

0 Answers0