Skip to content

Effortlessly Merge DataFrames in Python

[

Combining Data in pandas With merge(), .join(), and concat()

by Kyle Stratis

Table of Contents

  • pandas merge(): Combining Data on Common Columns or Indices
    • How to Use merge()
    • Examples
  • pandas .join(): Combining Data on a Column or Index
    • How to Use .join()
    • Examples
  • pandas concat(): Combining Data Across Rows or Columns
    • How to Use concat()
    • Examples
  • Conclusion

The Series and DataFrame objects in pandas are powerful tools for exploring and analyzing data. Part of their power comes from a multifaceted approach to combining separate datasets. With pandas, you can merge, join, and concatenate your datasets, allowing you to unify and better understand your data as you analyze it.

In this tutorial, you’ll learn how and when to combine your data in pandas with:

  • merge() for combining data on common columns or indices
  • .join() for combining data on a key column or an index
  • concat() for combining DataFrames across rows or columns

If you have some experience using DataFrame and Series objects in pandas and you’re ready to learn how to combine them, then this tutorial will help you do exactly that. If you’re feeling a bit rusty, then you can watch a quick refresher on DataFrames before proceeding.

pandas merge(): Combining Data on Common Columns or Indices

The first technique that you’ll learn is merge(). You can use merge() anytime you want functionality similar to a database’s join operations. It’s the most flexible of the three operations that you’ll learn.

When you want to combine data objects based on one or more keys, similar to what you’d do in a relational database, merge() is the tool you need. More specifically, merge() is most useful when you want to combine rows that share data.

You can achieve both many-to-one and many-to-many joins with merge(). In a many-to-one join, one of your datasets will have many rows in the merge column that repeat the same values. For example, the values could be 1, 1, 3, 5, and 5. At the same time, the merge column in the other dataset won’t have repeated values. Take 1, 3, and 5 as an example.

As you might have guessed, in a many-to-many join, both of your merge columns will have repeated values. These merges are more complex and result in the Cartesian product of the joined rows. This means that, after the merge, you’ll have every combination of rows that share the same value in the key column.

How to Use merge()

To use merge(), you’ll need two DataFrames that you want to combine based on a common column or index.

Here’s a step-by-step guide on how to use merge():

  1. Import the pandas library.
import pandas as pd
  1. Create two sample DataFrames.
df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'],
'value1': [1, 2, 3, 4]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'],
'value2': [5, 6, 7, 8]})
  1. Use merge() to combine the DataFrames based on the common column.
merged_df = pd.merge(df1, df2, on='key')

In this example, the resulting DataFrame merged_df will contain rows where the values in the ‘key’ column of both DataFrames match.

Examples

Here are a few examples to illustrate how merge() works.

Example 1: Many-to-One Merge

df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'],
'value1': [1, 2, 3, 4]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'],
'value2': [5, 6, 7, 8]})
merged_df = pd.merge(df1, df2, on='key')

The resulting merged DataFrame, merged_df, will look like this:

keyvalue1value2
B25
D46

Example 2: Many-to-Many Merge

df1 = pd.DataFrame({'key': ['A', 'A', 'B', 'B', 'C'],
'value1': [1, 2, 3, 4, 5]})
df2 = pd.DataFrame({'key': ['A', 'B', 'B', 'C', 'C'],
'value2': [6, 7, 8, 9, 10]})
merged_df = pd.merge(df1, df2, on='key')

The resulting merged DataFrame, merged_df, will look like this:

keyvalue1value2
A16
A26
B37
B38
B47
B48
C59
C510

Conclusion

Combining data in pandas is a powerful tool for analyzing and understanding your data. By using merge(), .join(), and concat(), you can combine datasets based on common columns or indices, resulting in a more comprehensive analysis. Whether you’re performing many-to-one or many-to-many joins, pandas provides a flexible and efficient way to combine your data.