I have several df:
df1 with columns A,B,C
df2 with columns A,B,C,D
df3 with columns A,B,C,E
How do I merge 3 dfs togerther so the result will contains columns A,B,C,D,E
My code currently is:
def merge_results(base_df, dfs, how = 'outer'):
nonempty_df = [df for df in dfs if not df.empty]
join_column = base_df.columns.tolist()
for df in [base_df] + nonempty_df:
for col in join_column:
if col in df.columns:
df[col] = df[col].astype(base_df[col].dtype)
merged_df = base_df.copy()
for df in nonempty_df:
merged_df_all = pd.merge(merged_df,df, on= join_column, how = how)
return merged_df_all
The results is only merge with furthest df, which results is only df_final[‘A’,’B’,’C’,’E’]
New contributor
Minh Hải is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3