Python

Python Pandas merge only certain columns

27 September 2026 · 9 min read

Python Pandas merge only certain columns

Data manipulation is a crucial aspect of data science, and Python’s Pandas library provides powerful tools for this purpose. One common task is merging datasets, but often you only need to merge based on specific columns. Mastering how to perform a Python Pandas merge only certain columns allows for more efficient and targeted data integration, preventing unnecessary data duplication and ensuring data integrity. This article will guide you through the process of merging dataframes in Pandas using selected columns, providing clear explanations, practical examples, and best practices to optimize your data workflows. Whether you’re a beginner or an experienced data analyst, understanding this technique will significantly enhance your ability to handle complex datasets.

Understanding Pandas Merge Operations

The Pandas merge() function is a versatile tool for combining dataframes based on shared columns. It operates similarly to SQL joins, allowing you to perform inner, outer, left, and right merges. However, when dealing with large datasets or when only a subset of columns is relevant for merging, specifying which columns to use becomes essential. Failing to do so can lead to incorrect results, increased processing time, and unnecessary memory consumption. Knowing how to target specific columns improves the efficiency and accuracy of your data merging tasks. This is a critical skill for any data professional using Python and Pandas.

By default, pd.merge() tries to infer which columns to use as merge keys based on common column names between the two DataFrames. While this is convenient in simple cases, it becomes problematic when DataFrames have multiple columns with the same name, but only some should be used for the merge. You might also want to merge on columns that have different names in the two DataFrames. Specifying the on, left_on, and right_on parameters gives you complete control over the merge operation. These parameters ensure that the merge is performed accurately and efficiently, minimizing the risk of errors and improving performance.

For instance, imagine you have two datasets: one containing customer information (customer_id, name, address) and another containing order details (order_id, customer_id, product). You want to combine these datasets to analyze customer orders. Using pd.merge() without specifying the on parameter might lead to unintended consequences if there are other common columns. Correctly specifying on='customer_id' ensures that the merge is performed only based on the customer ID, providing a clean and accurate combined dataset. According to Pandas documentation, “Explicit is better than implicit,” and this principle holds true when merging DataFrames [1].

Merging on a Single Column

The simplest scenario for a Python Pandas merge only certain columns is when you want to merge two dataframes based on a single shared column. This is achieved using the on parameter in the pd.merge() function. This parameter tells Pandas which column to use as the key for merging the dataframes. The on parameter is straightforward to use and provides a clear way to specify the merge column, enhancing code readability and reducing the potential for errors. This approach is particularly useful when dealing with dataframes that have a well-defined primary key relationship.

Let’s illustrate this with an example. Suppose you have two dataframes, df1 and df2, both containing a column named ‘ID’. To merge these dataframes based on the ‘ID’ column, you would use the following code:

python import pandas as pd Sample DataFrames data1 = {‘ID’: [1, 2, 3, 4], ‘Value1’: [‘A’, ‘B’, ‘C’, ‘D’]} data2 = {‘ID’: [1, 2, 5, 6], ‘Value2’: [‘X’, ‘Y’, ‘Z’, ‘W’]} df1 = pd.DataFrame(data1) df2 = pd.DataFrame(data2) Merge based on ‘ID’ column merged_df = pd.merge(df1, df2, on=‘ID’, how=‘inner’) print(merged_df) In this example, how='inner' specifies an inner join, which means that only rows with matching ‘ID’ values in both dataframes will be included in the merged dataframe. Other options for the how parameter include ‘outer’, ’left’, and ‘right’, each providing different ways to handle unmatched rows. This simple example demonstrates the power and flexibility of the on parameter in achieving precise and controlled merges.

The featured snippet-optimized paragraph: To merge Pandas DataFrames based on a specific column, use the pd.merge() function with the on parameter. Specify the name of the shared column as the value for the on parameter (e.g., pd.merge(df1, df2, on=‘ID’)). This ensures that the merge operation is performed using only the specified column as the key, resulting in a combined DataFrame with matching rows based on the values in the selected column. This is a simple yet powerful technique for precise data integration in Pandas.

Merging on Multiple Columns

Sometimes, a single column isn’t enough to uniquely identify rows for merging. In such cases, you might need to use multiple columns to perform a Python Pandas merge only certain columns. This involves specifying a list of column names to the on parameter. Merging on multiple columns allows for more granular and accurate data integration, especially when dealing with complex datasets that require composite keys. By combining multiple columns, you can ensure that the merged data is consistent and reliable.

Consider two dataframes, df3 and df4, with columns ‘ID’ and ‘Date’. To merge these dataframes based on both ‘ID’ and ‘Date’, you would use the following code:

python import pandas as pd Sample DataFrames data3 = {‘ID’: [1, 1, 2, 2], ‘Date’: [‘2023-01-01’, ‘2023-01-02’, ‘2023-01-01’, ‘2023-01-02’], ‘Value3’: [‘P’, ‘Q’, ‘R’, ‘S’]} data4 = {‘ID’: [1, 1, 2, 2], ‘Date’: [‘2023-01-01’, ‘2023-01-02’, ‘2023-01-01’, ‘2023-01-03’], ‘Value4’: [‘U’, ‘V’, ‘W’, ‘X’]} df3 = pd.DataFrame(data3) df4 = pd.DataFrame(data4) Merge based on ‘ID’ and ‘Date’ columns merged_df = pd.merge(df3, df4, on=[‘ID’, ‘Date’], how=‘inner’) print(merged_df) In this example, the merge will only include rows where both the ‘ID’ and ‘Date’ values match between the two dataframes. This ensures a more precise merge compared to using only one column. As pointed out in “Python for Data Analysis” by Wes McKinney, merging on multiple columns is a common practice in real-world data analysis [2]. It allows you to combine data from different sources based on a composite key, which is often necessary for maintaining data integrity and accuracy.

Merging with Different Column Names

Sometimes, the columns you want to merge on have different names in the two dataframes. In this scenario, you can use the left_on and right_on parameters to specify the respective column names. This is particularly useful when dealing with data from different sources that use different naming conventions. Using left_on and right_on allows you to seamlessly merge dataframes even when the column names don’t match, ensuring that you can combine data from various sources without requiring extensive data cleaning or renaming.

For instance, consider df5 with a column named ‘CustomerID’ and df6 with a column named ‘CustID’. To merge these dataframes based on these columns, you would use the following code:

python import pandas as pd Sample DataFrames data5 = {‘CustomerID’: [101, 102, 103, 104], ‘Value5’: [‘E’, ‘F’, ‘G’, ‘H’]} data6 = {‘CustID’: [101, 102, 105, 106], ‘Value6’: [‘Y’, ‘Z’, ‘A’, ‘B’]} df5 = pd.DataFrame(data5) df6 = pd.DataFrame(data6) Merge based on ‘CustomerID’ and ‘CustID’ columns merged_df = pd.merge(df5, df6, left_on=‘CustomerID’, right_on=‘CustID’, how=‘inner’) print(merged_df) In this example, left_on='CustomerID' specifies the column from df5 to use for merging, and right_on='CustID' specifies the column from df6. Note that the resulting merged dataframe will contain both ‘CustomerID’ and ‘CustID’ columns. You might want to drop one of them after the merge if they are redundant. According to a Stack Overflow discussion on Pandas merges, using left_on and right_on is the most efficient way to merge dataframes with different column names [3].

Here are some key takeaways for merging with different column names:

  • Use left_on to specify the column name in the left dataframe.
  • Use right_on to specify the column name in the right dataframe.
  • The resulting dataframe will contain both original columns.

Practical Examples and Best Practices

To solidify your understanding of Python Pandas merge only certain columns, let’s explore some practical examples and best practices. These examples will demonstrate how to apply the techniques discussed earlier in real-world scenarios, helping you to avoid common pitfalls and optimize your data merging workflows. By following these best practices, you can ensure that your data merges are accurate, efficient, and maintainable.

Suppose you have two datasets: one containing employee information (employee_id, name, department) and another containing salary information (employee_id, salary, bonus). You want to merge these datasets to analyze employee compensation by department. You can use the following code:

python import pandas as pd Sample DataFrames employee_data = {’employee_id’: [1, 2, 3, 4], ’name’: [‘Alice’, ‘Bob’, ‘Charlie’, ‘David’], ‘department’: [‘Sales’, ‘Marketing’, ‘Engineering’, ‘Sales’]} salary_data = {’employee_id’: [1, 2, 3, 4], ‘salary’: [60000, 70000, 80000, 65000], ‘bonus’: [5000, 6000, 7000, 5500]} employee_df = pd.DataFrame(employee_data) salary_df = pd.DataFrame(salary_data) Merge based on ’employee_id’ column merged_df = pd.merge(employee_df, salary_df, on=‘employee_id’, how=‘inner’) Group by department and calculate average salary department_salary = merged_df.groupby(‘department’)[‘salary’].mean() print(department_salary) This example demonstrates how to merge two dataframes based on a common column and then perform further analysis on the merged data. It showcases the power of Pandas in combining data from different sources and performing complex data analysis tasks. Here’s an ordered list of steps for efficient merging:

  1. Inspect your dataframes to identify common columns.
  2. Determine the appropriate merge type (inner, outer, left, right).
  3. Use the on, left_on, and right_on parameters to specify the merge columns.
  4. Verify the merged data to ensure accuracy.
  5. Clean up any redundant columns or missing values.
Infographic here
FAQ: Python Pandas Merge ------------------------
What is the difference between inner, outer, left, and right merges?
An inner merge returns only the rows that have matching values in both dataframes. An outer merge returns all rows from both dataframes, filling in missing values with NaN. A left merge returns all rows from the left dataframe and the matching rows from the right dataframe. A right merge returns all rows from the right dataframe and the matching rows from the left dataframe.
How do I handle duplicate column names after a merge?
Pandas automatically adds suffixes to duplicate column names (e.g., \_x, \_y). You can customize these suffixes using the `suffixes` parameter in `pd.merge()`. Alternatively, you can rename the columns after the merge.
Can I merge more than two dataframes at once?
< **Question & Answer :** Is it possible to only merge some columns? I have a DataFrame df1 with columns x, y, z, and df2 with columns x, a ,b, c, d, e, f, etc.

I want to merge the two DataFrames on x, but I only want to merge columns df2.a, df2.b - not the entire DataFrame.

The result would be a DataFrame with x, y, z, a, b.

I could merge then delete the unwanted columns, but it seems like there is a better method.

You want to use TWO brackets, so if you are doing a VLOOKUP sort of action:

df = pd.merge(df,df2[['Key_Column','Target_Column']],on='Key_Column', how='left') 

This will give you everything in the original df + add that one corresponding column in df2 that you want to join.