Merge multiple DataFrames Pandas

后端 未结 5 821
無奈伤痛
無奈伤痛 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:31

    Why not concatenate all the Data Frames, melt, then reform them using your ids? There might be a more efficient way to do this, but this works.

    df=pd.melt(pd.concat([df1,df2,df3]),id_vars=['profile','depth'])
    df_pivot=df.pivot_table(index=['profile','depth'],columns='variable',values='value')
    

    Where df_pivot will be

    variable              VAR1     VAR2    VAR3
    profile   depth                            
    profile_1 0.5    38.196202      NaN     NaN
              0.6    38.198002  0.20440     NaN
              1.1          NaN  0.20442     NaN
              1.2          NaN  0.20446  15.188
              1.3    38.200001      NaN  15.182
              1.4          NaN      NaN  15.182
    

提交回复
热议问题