Merge multiple dataframs 1

Merge multiple dataframs

# compile the list of dataframes you want to merge
data_frames = [df1, df2, df3]

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')

Here is what the above code is Doing:
1. data_frames is a list of dataframes you want to merge
2. reduce() is a function that takes in a function and a list as arguments.
3. lambda is a function that can take any number of arguments, but can only have one expression.
4. pd.merge() is a function in pandas that will merge two dataframes together.
5. The left and right arguments take in the dataframes you want to merge.
6. The on argument takes in the column you want to merge on.
7. The how argument takes in the type of merge you want to do.
8. The final product is a merged dataframe.

Similar Posts