~ 6 min read

Merging DataFrames with Pandas: A Step-by-Step Guide

By: Adam Richardson
Share:

Understanding Pandas Merge Function

One of the most powerful features of Pandas library is the ability to merge DataFrames. The merge() function in Pandas provides a powerful way to join two DataFrames by one or more keys, known as “join keys”. The merge() function is used to merge two or more pandas dataframes into a single dataframe based on a relevant column for the join.

Syntax

Pandas merge() function has the following syntax:

pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
         left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'),
         copy=True, indicator=False, validate=None)
  • left, right: DataFrames to be merged.
  • how: Type of merge to be performed (left, right, outer, or inner). Default is inner join.
  • on: Column to join on. Must be found in both DataFrames.
  • left_on, right_on: Columns to join from the left DataFrame and right DataFrame, respectively.
  • left_index, right_index: Whether to use the index as the join key. Default is False.
  • sort: Whether to sort the joined data. Default is True.
  • suffixes: Suffixes to apply to overlapping column names. Default is ‘_x’ and ‘_y’.
  • copy: Whether to copy data. Default is True.
  • indicator: Adds a column to the output DataFrame called _merge which indicates the source of each row. Default is False.
  • validate: Checks if merge is of specified type.

Types of Merge

  • Inner Merge: Returns only the rows that match in both DataFrames. Default merge type.
  • Left Merge: Returns all the rows from the left DataFrame and the matched rows from the right DataFrame.
  • Right Merge: Returns all the rows from the right DataFrame and the matched rows from the left DataFrame.
  • Outer Merge: Returns all rows and columns from both DataFrames. Rows with unmatched values are filled with NULL values.

Example

import pandas as pd

df1 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'A': ['a0', 'a1', 'a2', 'a3'],
                   'B': ['b0', 'b1', 'b2', 'b3']})

df2 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'C': ['c0', 'c1', 'c2', 'c3'],
                   'D': ['d0', 'd1', 'd2', 'd3']})

pd.merge(df1, df2, on='key')

###########################

    key     A   B   C   D
0   k0      a0  b0  c0  d0
1   k1      a1  b1  c1  d1
2   k2      a2  b2  c2  d2
3   k3      a3  b3  c3  d3

In the example above, we have two DataFrames with same ‘key’ columns. Using pd.merge() function, we combined both DataFrames based on ‘key’ column.

In conclusion, Pandas merge() function provides an easy and powerful way to join two DataFrames. It has various parameters to adjust the type of join we want to perform and provides various options to select keys for join.

Different Types of Merge Techniques

Merging of DataFrames is a common operation in data analysis. Different types of merge techniques are available in Pandas that help to combine DataFrames based on different criteria. In this section, we will go over each technique using examples.

Inner Merge

Inner merge returns only the rows that match in both DataFrames. This is the default type of join in Pandas merge() function.

import pandas as pd

df1 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'A': ['a0', 'a1', 'a2', 'a3'],
                   'B': ['b0', 'b1', 'b2', 'b3']})

df2 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'C': ['c0', 'c1', 'c2', 'c3'],
                   'D': ['d0', 'd1', 'd2', 'd3']})

pd.merge(df1, df2, on='key')

In the example above, we have two DataFrames with same ‘key’ columns. We merged the DataFrames based on ‘key’ column using pd.merge() function. Since inner merge is the default, the resulting DataFrame contains only the rows that matched in both DataFrames.

Left Merge

Left merge returns all the rows from the left DataFrame with the matched rows from the right DataFrame.

import pandas as pd

df1 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'A': ['a0', 'a1', 'a2', 'a3'],
                   'B': ['b0', 'b1', 'b2', 'b3']})

df2 = pd.DataFrame({'key': ['k1', 'k2', 'k3', 'k4'],
                   'C': ['c1', 'c2', 'c3', 'c4'],
                   'D': ['d1', 'd2', 'd3', 'd4']})

pd.merge(df1, df2, on='key', how='left')

In the example above, we have two DataFrames with different ‘key’ columns. Using pd.merge() function with how='left' parameter, we performed left merge. The resulting DataFrame contains all the rows from the left DataFrame with the matched rows from the right DataFrame.

Right Merge

Right merge returns all the rows from the right DataFrame with the matched rows from the left DataFrame.

import pandas as pd

df1 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'A': ['a0', 'a1', 'a2', 'a3'],
                   'B': ['b0', 'b1', 'b2', 'b3']})

df2 = pd.DataFrame({'key': ['k1', 'k2', 'k3', 'k4'],
                   'C': ['c1', 'c2', 'c3', 'c4'],
                   'D': ['d1', 'd2', 'd3', 'd4']})

pd.merge(df1, df2, on='key', how='right')

In the example above, we have two DataFrames with different ‘key’ columns. Using pd.merge() function with how='right' parameter, we performed right merge. The resulting DataFrame contains all the rows from the right DataFrame with the matched rows from the left DataFrame.

Outer Merge

Outer merge returns all rows and columns from both DataFrames. Rows with unmatched values are filled with NULL values.

import pandas as pd

df1 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'A': ['a0', 'a1', 'a2', 'a3'],
                   'B': ['b0', 'b1', 'b2', 'b3']})

df2 = pd.DataFrame({'key': ['k1', 'k2', 'k3', 'k4'],
                   'C': ['c1', 'c2', 'c3', 'c4'],
                   'D': ['d1', 'd2', 'd3', 'd4']})

pd.merge(df1, df2, on='key', how='outer')

In the example above, we have two DataFrames with different ‘key’ columns. Using pd.merge() function with how='outer' parameter, we performed outer merge. The resulting DataFrame contains all rows and columns from both DataFrames.

In conclusion, using different merge techniques in Pandas allows us to join DataFrames based on specific criteria with ease. By understanding the different types of merge techniques, we can choose the appropriate technique for our use case.

Handling Duplicate Values and Missing Data

When merging DataFrames, we may come across duplicate values and missing data. Pandas library provides different methods to handle these situations.

Handling Duplicate Values

Duplicate values can cause issues when merging DataFrames. Pandas provides duplicated() method to identify duplicate rows in a DataFrame.

import pandas as pd

df = pd.DataFrame({'key': ['k0', 'k0', 'k1', 'k2'],
                   'A': ['a0', 'a1', 'a2', 'a3'],
                   'B': ['b0', 'b1', 'b2', 'b3']})

df.duplicated(subset='key')

In the example above, we have a DataFrame with duplicate values in ‘key’ column. Using duplicated() method with subset='key' parameter, we identify rows that have duplicated ‘key’ values.

To drop duplicate values, we use drop_duplicates() method.

df.drop_duplicates(subset='key')

In the example above, we used drop_duplicates() method with subset='key' parameter to drop rows that have duplicated ‘key’ values.

Handling Missing Data

Missing data in DataFrames can cause issues while merging. Pandas provides fillna() method to replace missing values in a DataFrame.

import pandas as pd
import numpy as np

df1 = pd.DataFrame({'key': ['k0', 'k1', 'k2', 'k3'],
                   'A': [1, 2, 3, np.nan],
                   'B': [4, 5, np.nan, 7]})

df2 = pd.DataFrame({'key': ['k1', 'k2', 'k3', 'k4'],
                   'C': [10, 20, np.nan, 40],
                   'D': [30, np.nan, 50, 60]})

pd.merge(df1, df2, on='key').fillna(0)

In the example above, we have two DataFrames with missing values. Using fillna() method with parameter 0, we replaced the missing values with 0.

In conclusion, Pandas provides various methods to handle duplicate values and missing data in DataFrames. With the help of these methods, we can process DataFrames effectively and accurately.

Summary

Merging DataFrames in Pandas is an important skill for data analysts and scientists. In this article, we learned about different types of merge techniques, handling duplicate values and missing data while merging DataFrames. Understanding these concepts can help to improve data analysis skills and make it easier to combine datasets for better insights.

In my experience, learning Pandas for data analysis has been immensely helpful in streamlining data cleaning and exploration processes. Familiarizing oneself with the various merge, join and concat functions can make collaborations more streamlined and research more efficient.

Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts