Pandas Dataframes merged on two columns

Pandas – Merge DataFrames on Multiple Columns

In this tutorial, we’ll look at how to merge pandas dataframes on multiple columns.

The pandas merge() function is used to do database-style joins on dataframes. To merge dataframes on multiple columns, pass the columns to merge on as a list to the on parameter of the merge() function. The following is the syntax:

df_merged = pd.merge(df_left, df_right, on=['Col1', 'Col2', ...], how='inner')

Note that, the list of columns passed must be present in both the dataframes. If the column names are different in the two dataframes, use the left_on and right_on parameters to pass your column lists to merge on.

Let’s look at an example of using the merge() function to join dataframes on multiple columns. First, let’s create two dataframes that we’ll be joining together.

import pandas as pd

# monthly users
df_users = pd.DataFrame({
    'Year': [2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2020,2020,2020],
    'Quarter': ['Q1','Q1','Q1','Q2','Q2','Q2','Q3','Q3','Q3','Q4','Q4','Q4','Q1','Q1','Q1'],
    'Month': ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar'],
    'Users': [150,170,160,200,190,196,210,225,260,210,212,219,630,598,321]
})

# advertising partners
df_ad_partners = pd.DataFrame({
    'Year': [2019,2019,2019,2019,2020],
    'Quarter': ['Q1','Q2','Q3','Q4','Q1']
})


The dataframe df_users shows the monthly user count of an online store whereas the table df_ad_partners shows which ad partner was handling the store’s advertising.

YearQuarterMonthUsers
2019Q1Jan150
2019Q1Feb170
2019Q1Mar160
2019Q2Apr200
2019Q2May190
2019Q2Jun196
2019Q3Jul210
2019Q3Aug225
2019Q3Sep260
2019Q4Oct210
2019Q4Nov212
2019Q4Dec219
2020Q1Jan630
2020Q1Feb598
2020Q1Mar521
df_users
YearQuarterAd Partner
2019Q1A
2019Q2A
2019Q3A
2019Q4A
2020Q1B
df_ad_partners

If we want to include the advertising partner info alongside the users dataframe, we’ll have to merge the dataframes using a left join on columns “Year” and “Quarter” since the advertising partner information is unique at the “Year” and “Quarter” level.

df_merged = pd.merge(df_users, df_ad_partners, on=['Year', 'Quarter'], how='left')


This is the dataframe we get on merging –

YearQuarterMonthUsersAd Partner
2019Q1Jan150A
2019Q1Feb170A
2019Q1Mar160A
2019Q2Apr200A
2019Q2May190A
2019Q2Jun196A
2019Q3Jul210A
2019Q3Aug225A
2019Q3Sep260A
2019Q4Oct210A
2019Q4Nov212A
2019Q4Dec219A
2020Q1Jan630B
2020Q1Feb598B
2020Q1Mar321B
df_merged


You can see the Ad Partner info alongside the users count. An interesting observation post the merge is that there has been an increase in users since the switch from A to B as the advertising partner.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

In the above example, we saw how to merge two pandas dataframes on multiple columns. The columns to merge on had the same names across both the dataframes. It can happen that sometimes the merge columns across dataframes do not share the same names. In that case, you can use the left_on and right_on parameters to pass the list of columns to merge on from the left and right dataframe respectively.

For a complete list of pandas merge() function parameters, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on combining data in pandas –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top