Merge multiple DataFrames Pandas

后端 未结 5 819
無奈伤痛
無奈伤痛 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条回答
  •  抹茶落季
    2020-12-14 10:32

    Consider setting index on each data frame and then run the horizontal merge with pd.concat:

    dfs = [df.set_index(['profile', 'depth']) for df in [df1, df2, df3]]
    
    print(pd.concat(dfs, axis=1).reset_index())
    #      profile  depth       VAR1     VAR2    VAR3
    # 0  profile_1    0.5  38.198002      NaN     NaN
    # 1  profile_1    0.6  38.198002  0.20440     NaN
    # 2  profile_1    1.1        NaN  0.20442     NaN
    # 3  profile_1    1.2        NaN  0.20446  15.188
    # 4  profile_1    1.3  38.200001      NaN  15.182
    # 5  profile_1    1.4        NaN      NaN  15.182
    

提交回复
热议问题