0

I am trying to update DF1 with values from DF2['city'] , both tables have different size, and all are in pandas df.

DF1:

name  years  job
miki  21     worker
adam  22     PM
mike  51     director
wiola 33     manager

DF2:

name  years  city
miki  21     NY
adam  22     CAN

Result:

DF1:
name  years  job       city
miki  21     worker    NY
adam  22     PM        CAN
mike  51     director
wiola 33     manager

I need to take into account only name and years columns, if they are same as in second table , just take city value.

I have tried to do: DF1[DF1['name']==DF2['name'],'city']=DF2['city']

but its only for one condition city, i can do separatelly sam for years But main problem is that i cant do that for different tables , i am receiving error: ValueError: Can only compare identically-labeled Series objects

could anyone help me?

sygneto
  • 1,555
  • 7
  • 20
  • `df1.merge(df2, how='left', on=['name', 'years'])` – cs95 Dec 17 '18 at 12:26
  • hi, i am receiving error : `ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat` – sygneto Dec 17 '18 at 12:31
  • Then try `cols = ['name',' years']; pd.concat([df1.set_index(cols), df2.set_index(cols)], axis=1), join='outer')` – cs95 Dec 17 '18 at 12:33
  • now i am receiving : `pandas.core.indexes.base.InvalidIndexError: Reindexing only valid with uniquely valued Index objects` , also i deleted one `)` after `axis=1` – sygneto Dec 17 '18 at 12:44
  • 1
    Then heed the warning and fix the error: `df1['years'] = pd.to_numeric(df1['years'], errors='coerce'); df2['years'] = pd.to_numeric(df2['years'], errors='coerce'); df1.merge(df2, how='left', on=['name', 'years'])` – cs95 Dec 17 '18 at 12:45

0 Answers0