Home > Software design >  how to merge in pandas with 4 columns
how to merge in pandas with 4 columns

Time:01-28

I have 2 Dataframe in pandas. I want to merge but, I need to do it by 4 columns. Somenthing like that:

DF 1:

doc, cod_doc, name, city

DF 2:

id, cod_id, income

I need doing merge for doc = id and cod_doc = cod_id.

This is my code:

f = pd.merge(customers_adress, customers_income,left_on='doc',right_on='id',how='left') 

CodePudding user response:

you can merge on multiple columns, sounds like you want to do

df = pd.merge(customers_adress, customers_income,left_on=['doc','cod_doc'],right_on=['id','cod_id'],how='left')

without having your dfs it is hard to say if this is exactly what you want but it should work

CodePudding user response:

f = pd.merge(
customers_adress, customers_income, 
left_on=['doc', 'cod_doc'],
right_on=['id', 'cod_id',
how='left'
) 
  •  Tags:  
  • Related