I've been searching for help with a merging query but I'm not sure of the most succinct way of phrasing my question, so haven't been able to find anything that helps directly.
I'm looking to merge two tables on three columns using a left join, however there is some null entries in the third field and in this case, I think I effectively want an outer join - so if there is a match on the first two columns, the data will be joined regardless of whether there is a match in the third column.
df_A:
Competitor Product Type
A P1 X
A P2 X
A P2 Y
B P1 X
B P1 Y
df_B:
Competitor Product Type Value
A P1 X £5
A P2 X £10
A P2 Y £12
B P1 £15
I want to do a merge on these two tables using the Competitor, Product and Type fields. However, the 'Type' field in the second table will not always be populated and in this instance I want the Value to be applied to all Types in table A, i.e:
Competitor Product Type Value
A P1 X £5
A P2 X £10
A P2 Y £12
B P1 X £15
B P1 Y £15
I can successfully merge on the first two columns using the code:
df_merge=pd.merge(df_A,df_B,how='left',on=['Competitor','Product'])
but if I add the third column 'Type', this only populates the Value for those with matches in all columns, i.e.:
Competitor Product Type Value
A P1 X £5
A P2 X £10
A P2 Y £12
B P1
B P1
Is there a way of combining the join types, or any other way of getting to this solution?