Skip to main content

df.merge()

df.merge(right, on=column, left_on=left_column, right_on=right_column, left_index=False, right_index=False)

Merges two DataFrames by specified columns or indexes.

Input:
right : DataFrame
DataFrame to merge with.
on : label or list
Column or index names to join on.
left_on : label, list, or array-like
Column or index names to join on the in the left DataFrame.
right_on : label, list, or array-like
Column or index names to join on the in the right DataFrame.
left_index : boolean, default False
Use index from left DataFrame as join key(s).
right_index : boolean, default False
Use index from right DataFrame as join key(s).
Returns:
df_merged - A new DataFrame with the two DataFrames merged on the specified input combinations.
Return Type:
DataFrame
Note:
  • The number of columns merged on the left and right DataFrames must be the same.
  • left_on and left_index cannot happen at the same time. Same as right_on and right_index.

pets_info = bpd.DataFrame().assign(
Pet_ID=['dog_001', 'cat_001', 'cat_002', 'dog_002', 'dog_003', 'ham_001'],
Breed=['Labrador', 'Sphinx', 'Siamese', 'Chihuahua', 'Labrador', 'Roborovski'],
Owner=['Jason', 'Lauren', 'Cassidy', 'Bobby', 'Dhruv', 'Cassidy']
)
pets_info
IndexPet_IDBreedOwner
0dog_001LabradorJason
1cat_001SphinxLauren
2cat_002SiameseCassidy
3dog_002ChihuahuaBobby
4dog_003LabradorDhruv
5ham_001RoborovskiCassidy
pets.merge(pets_info, left_on='ID', right_on='Pet_ID')
IndexIDSpeciesColorWeightAgeIs_CatOwner_CommentPet_IDBreedOwner
0dog_001dogblack405False There are no bad dogs, only bad owners.dog_001LabradorJason
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!cat_001SphinxLauren
2cat_002catblack159True****All you need is love and a cat.****cat_002SiameseCassidy
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.dog_002ChihuahuaBobby
4dog_003dogblack250.5FalseBe the person your dog thinks you are.dog_003LabradorDhruv
5ham_001hamsterblack13FalseNo, thank you!ham_001RoborovskiCassidy
pets_info = pets_info.set_index('Pet_ID')
pets.merge(pets_info, left_on='ID', right_index=True)
IndexIDSpeciesColorWeightAgeIs_CatOwner_CommentBreedOwner
0dog_001dogblack405False There are no bad dogs, only bad owners.LabradorJason
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!SphinxLauren
2cat_002catblack159True****All you need is love and a cat.****SiameseCassidy
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.ChihuahuaBobby
4dog_003dogblack250.5FalseBe the person your dog thinks you are.LabradorDhruv
5ham_001hamsterblack13FalseNo, thank you!RoborovskiCassidy
pets_info2 = pets_info.assign(ID = pets_info.index)
pets.merge(pets_info2, on='ID')
IndexIDSpeciesColorWeightAgeIs_CatOwner_CommentBreedOwner
0dog_001dogblack405False There are no bad dogs, only bad owners.LabradorJason
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!SphinxLauren
2cat_002catblack159True****All you need is love and a cat.****SiameseCassidy
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.ChihuahuaBobby
4dog_003dogblack250.5FalseBe the person your dog thinks you are.LabradorDhruv
5ham_001hamsterblack13FalseNo, thank you!RoborovskiCassidy