how to merge in pandas with 4 columns

Viewed 47

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') 
2 Answers
f = pd.merge(
customers_adress, customers_income, 
left_on=['doc', 'cod_doc'],
right_on=['id', 'cod_id',
how='left'
) 

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

Related