Python

How to merge multiple dataframes

25 September 2026 · 6 min read

How to merge multiple dataframes

In today’s data-driven world, information often resides in disparate sources, making comprehensive analysis a significant challenge. Whether you’re working with customer transaction logs, sensor data, or financial records, it’s rare to find all the necessary insights neatly packaged in a single file. This fragmentation necessitates robust methods for combining datasets, and one of the most fundamental skills for any data professional is understanding how to merge multiple dataframes effectively. Merging dataframes allows you to consolidate related information, creating a unified view that reveals deeper patterns and facilitates more insightful decision-making. This guide will walk you through the essential concepts, practical steps, and best practices for integrating your dataframes, transforming scattered data into a powerful, cohesive resource for your analytical endeavors.

Understanding the Core Concepts of Dataframe Merging

Merging dataframes is a powerful technique for combining two or more datasets based on common columns or indices. Unlike concatenation, which simply stacks dataframes on top of each other or side by side, merging intelligently aligns rows from different dataframes using a specified “key.” This key acts as the bridge between your datasets, ensuring that corresponding records are correctly matched. For instance, if you have one dataframe with customer IDs and their names, and another with customer IDs and their purchase history, merging on the ‘customer ID’ key will link each customer’s name to their respective transactions.

The process of data manipulation through merging is crucial in virtually every data science project. It’s akin to performing relational database joins, bringing together information from different tables. The choice of which columns to use as keys, and understanding the implications of different join types, are fundamental to achieving accurate and meaningful results. Without proper merging, data analysis can be incomplete or, worse, lead to erroneous conclusions due to misaligned or missing information. As data scientist Jake VanderPlas notes in “Python Data Science Handbook,” “Combining datasets is a common task in data analysis, and Pandas provides a rich set of tools for doing so efficiently.”

Exploring Different Types of Joins for Data Integration

When you merge dataframes, you must specify the type of join you want to perform. The four primary types—inner, left, right, and outer—determine how rows are handled when there isn’t a perfect match between the key columns in both dataframes. Understanding these distinctions is paramount for effective data integration and ensuring your merged output contains precisely the data you intend.

An inner join is the most common type; it returns only the rows where the key columns have matching values in both dataframes. Rows that do not have a match in both datasets are excluded from the result. This is ideal when you only care about the intersection of your data. Conversely, a left join (or left outer join) includes all rows from the “left” dataframe and any matching rows from the “right” dataframe. If there’s no match for a row in the left dataframe, the columns from the right dataframe will be filled with missing values (e.g., NaN in Pandas). The right join (or right outer join) operates symmetrically, keeping all rows from the “right” dataframe and matching rows from the “left.”

Finally, an outer join (or full outer join) returns all rows when there is a match in either the left or right dataframe. Where there is no match, it fills in missing values for the corresponding columns. This type of join is useful when you want to retain all information from both datasets, even if some records don’t have direct counterparts. For example, if you’re combining a list of all employees with a list of employees who completed a specific training, a left join would show all employees and their training status, while an outer join would show all employees from both lists, even if they only appeared in one. Choosing the correct join type is a critical step in any robust data analysis workflow.

Infographic here
Practical Steps to Merge Multiple Dataframes in Python with Pandas ------------------------------------------------------------------

For many data professionals, Python’s Pandas library is the go-to tool for joining data and performing complex data manipulations. The pd.merge() function is incredibly versatile for combining dataframes. Here’s a step-by-step guide on how to effectively use it to merge multiple dataframes:

  1. Identify Common Key Columns:

    Before merging, you must determine which columns (or set of columns) are common across the dataframes you wish to combine. These will serve as your join keys. For example, if you have customer dataframes, ‘CustomerID’ is a likely candidate. Ensure these columns have consistent naming and data types across all dataframes for accurate matching.

  2. Choose the Appropriate Join Type:

    Based on your analytical objective, select an how argument for the pd.merge() function: '<b>Question & Answer : </b><br></br><p>I have different dataframes and need to merge them together based on the date column. If I only had two dataframes, I could use df1.merge(df2, on='date'), to do it with three dataframes, I use df1.merge(df2.merge(df3, on='date'), on='date'), however it becomes really complex and unreadable to do it with multiple dataframes.</p> <p>All dataframes have one column in common -date, but they don't have the same number of rows nor columns and I only need those rows in which each date is common to every dataframe.</p> <p>So, I'm trying to write a recursion function that returns a dataframe with all data but it didn't work. How should I merge multiple dataframes then?</p> <p>I tried different ways and got errors like out of range, keyerror 0/1/2/3 and can not merge DataFrame with instance of type <class 'NoneType'>.</p> <p>This is the script I wrote:</p> <pre>dfs = [df1, df2, df3] # list of dataframes def mergefiles(dfs, countfiles, i=0): if i == (countfiles - 2): # it gets to the second to last and merges it with the last return dfm = dfs[i].merge(mergefiles(dfs[i+1], countfiles, i=i+1), on='date') return dfm print(mergefiles(dfs, len(dfs))) </pre> <p>An example: df_1:</p> <pre>May 19, 2017;1,200.00;0.1% May 18, 2017;1,100.00;0.1% May 17, 2017;1,000.00;0.1% May 15, 2017;1,901.00;0.1% </pre> <p>df_2:</p> <pre>May 20, 2017;2,200.00;1000000;0.2% May 18, 2017;2,100.00;1590000;0.2% May 16, 2017;2,000.00;1230000;0.2% May 15, 2017;2,902.00;1000000;0.2% </pre> <p>df_3:</p> <pre>May 21, 2017;3,200.00;2000000;0.3% May 17, 2017;3,100.00;2590000;0.3% May 16, 2017;3,000.00;2230000;0.3% May 15, 2017;3,903.00;2000000;0.3% </pre> <p>Expected merge result:</p> <pre>May 15, 2017; 1,901.00;0.1%; 2,902.00;1000000;0.2%; 3,903.00;2000000;0.3% </pre><br></br><h1>Short answer</h1> <pre class="lang-py prettyprint-override">df_merged = reduce(lambda left,right: pd.merge(left,right,on=['DATE'], how='outer'), data_frames) </pre> <h1>Long answer</h1> <p>Below, is the most clean, comprehensible way of merging multiple dataframe if complex queries aren't involved.</p> <p>Just simply merge with <strong>DATE</strong> as the index and merge using <strong>OUTER</strong> method (to get all the data).</p> <pre>import pandas as pd from functools import reduce df1 = pd.read_table('file1.csv', sep=',') df2 = pd.read_table('file2.csv', sep=',') df3 = pd.read_table('file3.csv', sep=',') </pre> <p>Now, basically load all the files you have as data frame into a list. And, then merge the files using merge or reduce function.</p> <pre># compile the list of dataframes you want to merge data_frames = [df1, df2, df3] </pre> <p><strong>Note: you can add as many data-frames inside the above list.</strong> This is the good part about this method. No complex queries involved.</p> <p>To keep the values that belong to the same date you need to merge it on the DATE</p> <pre>df_merged = reduce(lambda left,right: pd.merge(left,right,on=['DATE'], how='outer'), data_frames) # if you want to fill the values that don't exist in the lines of merged dataframe simply fill with required strings as df_merged = reduce(lambda left,right: pd.merge(left,right,on=['DATE'], how='outer'), data_frames).fillna('void') </pre> <ul> <li>Now, the output will the values from the same date on the same lines.</li> <li>You can fill the non existing data from different frames for different columns using fillna().</li> </ul> <p>Then write the merged data to the csv file if desired.</p> <pre>pd.DataFrame.to_csv(df_merged, 'merged.txt', sep=',', na_rep='.', index=False) </pre> <p>This should give you</p> <p>DATE VALUE1 VALUE2 VALUE3 ....</p>