I want to combine these 3 dataframes, based on their ID
columns, and get the below output. I am after a short way that I can use it for combining many more number of dataframes later.
Input:
+---+---+---+
| ID| a| b|
+---+---+---+
| A| 1| 1|
| B| 2| 2|
+---+---+---+
+---+---+---+
| ID| c| d|
+---+---+---+
| A| 3|333|
| B| 4|444|
+---+---+---+
+---+---+---+
| ID| e| f|
+---+---+---+
| A|555| 5|
| B|666| 6|
+---+---+---+
Output:
+---+---+---+---+---+---+---+
| ID| a| b| c| d| e| f|
+---+---+---+---+---+---+---+
| A| 1| 1| 3|333|555| 5|
| B| 2| 2| 4|444|666| 6|
+---+---+---+---+---+---+---+
Answer: For anyone who might find it useful later!
# create list of dataframes
list_df = [df1, df2, df3]
# merge all at once
df_all = reduce(lambda x, y: x.join(y, on="ID"), list_df)