Dataframe merging – left join by matching more than 1 column (multiple column matching)

Question:

I have a main DF1 like this

Name Type Price Quantity
Product A Phone 100 5
Product B TV 30 3

DF2

Name Type Image Shelf Position
Product A Phone image1 Shelf A
Product A TV image 2 Shelf B
Product A Speakers image 3 Shelf C
Product B Fridge image 4 Shelf A
Product B TV image 5 Shelf F

bI’m wondering if there is a neat solution to create a DF by LEFT JOIN DF2 with DF1 while matching by more than 1 column (in this case, the match will be on Name and Type)

Name Type Price Quantity Image Shelf Position
Product A Phone 100 5 image1 Shelf A
Product B TV 30 3 image 5 Shelf F

I was going to create a new combined column (name_type) and rely on it to left join but I’m hoping to find out if there is a better way?

Thank you

Asked By: Duyth

||

Answers:

The on parameter of pandas.DataFrame.merge accepts lists.

on: label or list
Column or index level names to join on. These must be
found in both DataFrames. If on is None and not merging on indexes
then this defaults to the intersection of the columns in both
DataFrames.

out = df1.merge(df2, on=["Name", "Type"], how="left")

# Output :

print(out)
        Name   Type  Price  Quantity    Image Shelf Position
0  Product A  Phone    100         5   image1        Shelf A
1  Product B     TV     30         3  image 5        Shelf F
Answered By: abokey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.