Home > Software engineering >  merging three data frames based on id
merging three data frames based on id

Time:04-04

The 3 dataFrames I have are as below: df_first = pd.read_csv("first.csv", index_col = "id") df_second = pd.read_csv("second.csv" , index_col = "id") df_third = pd.read_csv("third.csv" , index_col = "id")

I have indexed them by id as shown above. df_first has two colmns col_1 and col_2, while df_second has two colmns i.e. col_3 and col_4, and df_third has columns as col_5 and col_6. I want to create another dataFrame which has the col_2, col_4, col_6 and one more extra columns which is the sum of all the 3 entries in the row. By using python, how can I do it.

CodePudding user response:

You can combine the dataframes by calling DataFrame.merge e.g.

temp = df1.merge(df2, on='id')
final = df3.merge(temp, on='id')

To sum up row values, you can do as following:

final["sum"] = final[col_2]   final[col_4]   final[col_6]
  • Related