Home >Backend Development >Python Tutorial >How to Efficiently Merge Multiple DataFrames in Python?
Merging multiple dataframes can be complex and frustrating, especially when the number of dataframes increases. While it's possible to use nested merge functions, this approach becomes unmanageable and error-prone.
For a more elegant and efficient way to merge multiple dataframes, consider the following solution:
import pandas as pd from functools import reduce # Initialize a list of dataframes dfs = [df1, df2, df3] # Merge the dataframes using the reduce function df_merged = reduce(lambda left, right: pd.merge(left, right, on=['DATE'], how='outer'), dfs)
This solution utilizes the reduce function from the functools module to iteratively merge the dataframes. The lambda function defines the merge operation, using the specified column for joining ('DATE') and the 'outer' merge method to retain all rows.
Clean and Comprehensible: This solution provides a clear and concise approach to merging multiple dataframes, eliminating the need for complex nested merge functions.
Handle Multiple Dataframes Effectively: This solution can handle any number of dataframes, making it scalable and convenient.
Consider the following dataframes:
df_1: 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% df_2: 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% df_3: 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%
Using the solution provided, we can merge these dataframes:
df_merged = reduce(lambda left, right: pd.merge(left, right, on=['DATE'], how='outer'), dfs)
Result:
DATE VALUE1 VALUE2 VALUE3 May 15, 2017; 1,901.00;0.1%; 2,902.00;1000000;0.2%; 3,903.00;2000000;0.3%
The above is the detailed content of How to Efficiently Merge Multiple DataFrames in Python?. For more information, please follow other related articles on the PHP Chinese website!