Merge multiple DataFrames Pandas

后端 未结 5 826
無奈伤痛
無奈伤痛 2020-12-14 09:56

This might be considered as a duplicate of a thorough explanation of various approaches, however I can\'t seem to find a solution to my problem there due to a higher number

5条回答
  •  猫巷女王i
    2020-12-14 10:34

    A simple way is with a combination of functools.partial/reduce.

    Firstly partial allows to "freeze" some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. Then with reduce we can apply cumulatively the new partial object to the items of iterable (list of dataframes here):

    from functools import partial, reduce
    
    dfs = [df1, df2, df3]
    merge = partial(pd.merge, on=['depth', 'profile'], how='outer')
    reduce(merge, dfs)
    
       depth       VAR1    profile     VAR2    VAR3
    0    0.6  38.198002  profile_1  0.20440     NaN
    1    0.6  38.198002  profile_1  0.20440     NaN
    2    1.3  38.200001  profile_1      NaN  15.182
    3    1.1        NaN  profile_1  0.20442     NaN
    4    1.2        NaN  profile_1  0.20446  15.188
    5    1.4        NaN  profile_1      NaN  15.182
    

提交回复
热议问题