How to Left Joins with Multiple Keys in Pandas | Pandas | Dataframe || Python Tutorial

Опубликовано: 13 Март 2026
на канале: PythonTrailBlazer
23
1

Learn how to perform flexible left joins in Pandas when your join key needs to match multiple potential columns in another DataFrame.
This tutorial will guide you through a custom Python function to handle complex merging scenarios with ease.


Source Code:
+++++++++++++++++++++++++++++++++++++
import pandas as pd

df_sales = pd.DataFrame({
'transaction_id': [101, 102, 103, 104],
'product_id': ['P001', 'P002', 'P003', 'P004'],
'quantity': [5, 3, 6, 2]
})
print("Sales Data (df_sales):\n",df_sales)

df_product_info = pd.DataFrame({
'product_id': ['P001', 'P005', 'P007'],
'product_code': ['A001', 'P002', 'P008'],
'product_sku': ['S001', 'S002', 'P003'],
'product_name': ['Product 1', 'Product 2', 'Product 3']
})

print("Product Information (df_product_info)::\n",df_product_info)


def custom_left_join(df_left, df_right, left_key, right_keys):
"""
Perform a left join where the key from the left DataFrame can match any of several columns in the right DataFrame.

Parameters:
df_left (pd.DataFrame): The left DataFrame.
df_right (pd.DataFrame): The right DataFrame.
left_key (str): The column name in the left DataFrame to join on.
right_keys (list): A list of column names in the right DataFrame to match with the left_key.

Returns:
pd.DataFrame: The resulting DataFrame after the join.
"""
Create a unique name for the value column during the melt
temp_value_name = left_key + '_temp'

Melt the right DataFrame to reshape it for the join
df_right_melted = df_right.melt(id_vars=[col for col in df_right.columns if col not in right_keys],
value_vars=right_keys,
var_name='right_key',
value_name=temp_value_name)

Rename the temporary value column to the left_key name
df_right_melted = df_right_melted.rename(columns={temp_value_name: left_key})

Perform the merge
merged_df = pd.merge(df_left, df_right_melted, how='left', on=left_key)

Drop the intermediate column used for melting
merged_df = merged_df.drop(columns=['right_key'])

return merged_df


Perform the custom left join
result = custom_left_join(df_sales,
df_product_info,
'product_id',
['product_id',
'product_code',
'product_sku'])
print(result)
++++++++++++++++++++++++++++++
#Python
#Pandas
#DataScience
#DataFrame
#JoinOperations
#LeftJoin
#DataMerging
#PythonTutorial
#Programming
#DataAnalysis