pandas three-way joining multiple dataframes on columns

前端 未结 11 1762
醉梦人生
醉梦人生 2020-11-22 08:35

I have 3 CSV files. Each has the first column as the (string) names of people, while all the other columns in each dataframe are attributes of that person.

How can

11条回答
  •  清酒与你
    2020-11-22 09:07

    I tweaked the accepted answer to perform the operation for multiple dataframes on different suffix parameters using reduce and i guess it can be extended to different on parameters as well.

    from functools import reduce 
    
    dfs_with_suffixes = [(df2,suffix2), (df3,suffix3), 
                         (df4,suffix4)]
    
    merge_one = lambda x,y,sfx:pd.merge(x,y,on=['col1','col2'..], suffixes=sfx)
    
    merged = reduce(lambda left,right:merge_one(left,*right), dfs_with_suffixes, df1)
    

提交回复
热议问题