Pandas: Perform ‘SELF JOIN’ on a single DataFrame (4 examples)

Updated: February 24, 2024 By: Guest Contributor Post a comment


A ‘SELF JOIN’ in the context of SQL is a common database operation that involves joining a table with itself. This can be useful for comparing rows within the same table to find duplicates, perform hierarchical, sequential, or pairing operations based on some conditions. Similarly, in Pandas, a Python data analysis and manipulation library, performing a SELF JOIN involves using operations that allow one DataFrame to be merged with itself, to achieve complex data reshaping and analysis. This article explores four examples of performing a SELF JOIN in Pandas, moving from basic operations to more complex ones.

Example 1: Basic SELF JOIN

Initiate with a simple example where we demonstrate how to perform a basic SELF JOIN. Let’s start with creating a sample DataFrame:

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    'employee_id': [1, 2, 3, 4],
    'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
    'manager_id': [2, 3, 4, 1]


This data represents employees and their respective managers by their IDs. To perform a SELF JOIN that associates employees with their managers, we can apply the merge method:

joined_df = df.merge(df, left_on='employee_id', right_on='manager_id', suffixes=('_employee', '_manager'))

print(joined_df[['name_employee', 'name_manager']])

This simple SELF JOIN operation allows us to create a new DataFrame that maps employees to their managers, showing the hierarchical relationship within the dataset.

Example 2: SELF JOIN With Condition Filtering

Moving onto a slightly more complex example, let’s consider we want to identify pairs of employees based on a certain criteria. For this, we will introduce a helper column to the DataFrame:

df['experience_years'] = [3, 5, 2, 4]  # Adding experience years

# Filtering employees with at least 3 years difference in experience
filtered_join = df.merge(df, left_on='employee_id', right_on='employee_id', suffixes=('_left', '_right'))
filtered_join = filtered_join[filtered_join['experience_years_left'] >= (filtered_join['experience_years_right'] + 3)]

print(filtered_join[['name_left', 'name_right']])

This example demonstrates how a SELF JOIN combined with a filter for experience years can help in understanding relational dynamics within the data, such as mentorship or hierarchical structure based on experience.

Example 3: Advanced SELF JOIN for Hierarchical Data

As we dive deeper into more complex uses of SELF JOIN in Pandas, consider a scenario where we’re analyzing hierarchical data within an organization. Using a multi-level indexing or conditions that involve multiple fields can enhance the analysis significantly. For instance:

df.set_index('employee_id', inplace=True)

parent_child_df = df.merge(df, left_on='manager_id', right_index=True, suffixes=('_child', '_parent'))

print(parent_child_df[['name_child', 'name_parent']])

This example showcases the power of indexing and how it can be used to perform a SELF JOIN for hierarchical data exploration, revealing the organization’s structure in a more nuanced way.

Example 4: Combining SELF JOIN with GroupBy for Aggregation

Lastly, let’s explore an example where a SELF JOIN is combined with aggregation functions to provide summarized insights. Imagine we’re interested in finding out the average experience years of employees under each manager:

manager_experience = df.merge(df, left_on='manager_id', right_on='employee_id')
    'experience_years_left': 'mean'
}).rename(columns={'experience_years_left': 'avg_experience_years_under_manager'})


This complex operation highlights how SELF JOIN in Pandas can be leveraged alongside aggregation to yield profound insights, from superiors’ influence on their team’s development to general staff skill level analysis.


In this tutorial, we’ve explored various methods to perform a SELF JOIN in Pandas, from basic implementations to more advanced techniques that integrate filtering, hierarchical indexing, and aggregation for detailed analysis. By understanding these examples, users can apply similar methodologies to enhance their data analysis and manipulation tasks in Pandas, tailoring the approach to fit their unique datasets and questions.