I have three pandas dataframes: df1, df2, df3 which looks like as follows:
df1=
X | Y | M1_x | M2_x | M3_x |
---|---|---|---|---|
12 | 33 | 3 | 4 | 2 |
12 | 54 | 0 | 3 | 4 |
23 | 12 | 0 | 8 | 3 |
df2=
X | Y | M1_y | M2_y | M3_y |
---|---|---|---|---|
12 | 33 | 9 | 4 | 1 |
12 | 54 | 0 | 3 | 5 |
12 | 11 | 0 | 2 | 1 |
df3=
X | Y | M1_z | M2_z | M3_z |
---|---|---|---|---|
12 | 33 | 1 | 40 | 10 |
11 | 10 | 10 | 30 | 0 |
12 | 11 | 0 | 40 | 5 |
I like to concatenate the two df and get the merged dataframe as below:
result =
X | Y | M1_x | M2_x | M3_x | M1_y | M2_y | M3_y | M1_z | M2_z | M3_z |
---|---|---|---|---|---|---|---|---|---|---|
12 | 33 | 3 | 4 | 2 | 9 | 4 | 1 | 1 | 40 | 10 |
12 | 54 | 0 | 3 | 4 | 0 | 3 | 5 | nan | nan | nan |
23 | 12 | 0 | 8 | 3 | nan | nan | nan | nan | nan | nan |
12 | 11 | nan | nan | nan | 0 | 2 | 1 | 0 | 40 | 5 |
11 | 10 | nan | nan | nan | nan | nan | nan | 10 | 30 | 0 |
I already tried p.cocatenate function, the problem is choosing axis=1, doesn't merge the[X, Y] columns, then I have double [X,Y] columns in the result file, choosing axis=0, doesn't merge the rows, and I get the union of rows instead of merging common [X,Y] with eachother. How can I make this happen?
EDIT: I know how to use merge function when merging two dataframes. My problem here is I have more than two (actually 4) databases to merge. Is there any function to combine more than two dfs in one line?