Home > Software engineering >  is There any methods to merge multiple dataframes of different templates
is There any methods to merge multiple dataframes of different templates

Time:08-17

There are a total of 4 dataframes (df1 / df2 / df3 / df4),

Each dataframe has a different template, but they all have the same columns.

I want to merges the row of each dataframe based on the same column, but what function should I use? A 'merge' or 'join' function doesn't seem to work, and deleting the rest of the columns after grouping them into a list seems to be too messy.

I want to make attached image

enter image description here

CodePudding user response:

This is an option, you can merge the dataframes and then drop the useless columns from the total dataframe.

df_total = pd.concat([df1, df2, df3, df4], axis=0)
df_total.drop(['Value2', 'Value3'], axis=1)

CodePudding user response:

You can use reduce to get it done too.

from functools import reduce


reduce(lambda left,right: pd.merge(left, right, on=['ID','value1'], how='outer'), [df1,df2,df3,df4])[['ID','value1']]
  ID  value1
0  a       1
1  b       4
2  c       5
3  f       1
4  g       5
5  h       6
6  i       1
  • Related