Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging three dataframes with three similar indexes:

I have three dataframes

df1 :
Date           ID        Number     ID2   info_df1
2021-12-11     1         34         36       60
2021-12-10     2         33         35       57
2021-12-09     3         32         34       80
2021-12-08     4         3133       55

df2:
Date           ID        Number     ID2   info_df2
2021-12-10     2         18         20       50
2021-12-11     1         34         36       89
2021-12-10     2         33         35       40
2021-12-09     3         32         34       92

df3:
Date           ID        Number     ID2   info_df3
2021-12-10     2         18         20       57
2021-12-10     2         18         20       63
2021-12-11     1         34         36       52
2021-12-10     2         33         35       33

I need a data frame with info column from df1,df2 and df3 and Date,ID,Number,ID2 as index.

Format of the merged dataframe should consist these columns:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Date ID Number ID2 info_df1 info_df2
info_df3

>Solution :

If you trying to merge the dataframe based on Date, I think what you need is merge function:

mergedDf = df1.merge(df2, on="Date").merge(df3, on="Date");
mergedDf.set_index("ID2", inplace = True)

But if you are trying to merge dataframes based on multiple columns, you can use a list of column names on the on argument:

mergedDf = df1.merge(df2, on=["Date", "ID", "ID2"]).merge(df3, on=["Date", "ID", "ID2"]);
mergedDf.set_index("ID2", inplace = True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading