traggatmot
traggatmot

Reputation: 1463

Concat two Pandas dataframes without sorting on initial columns

I have two data frames:

Dataframe A

index  ID_1  ID_2   Value
0       1     1      1.0
1       1     2      1.2
2       1     3      1.3

DataFrame B

index  ID_1  ID_2   Value
0       1     2      1.0
1       1     1      1.2
2       1     3      1.3

And each is sorted by ID_1, then by Value.

I want to merge or concatenate the two dataframes so that I can easily determine where ID_2 in dataframe A and dataframe B are not the same - so determine when the sorting I have highlights a difference in the order of the ID_2 column.

I have tried to use pd.concat several different ways and I always get this result:

index  ID_1  ID_2   Value    ID_1  ID_2   Value
0       1     1      1.0      1     1      1.2
1       1     2      1.2      1     2      1.0
2       1     3      1.3      1     3      1.3  

Rather than what I want:

index  ID_1  ID_2   Value    ID_1  ID_2   Value
0       1     1      1.0      1     2      1.0
1       1     2      1.2      1     1      1.2
2       1     3      1.3      1     3      1.3  

Any ideas? I have tried it with different column names, etc.

Edit:

The solution proposed below of course works, but one must remember to do reset_index(drop=True) on the dataframe to have the indexes reset after the sorting.

Upvotes: 2

Views: 3308

Answers (1)

piRSquared
piRSquared

Reputation: 294278

this works for me
assuming 'index' is the index for both A and B

pd.concat([A, B], axis=1)

enter image description here

Upvotes: 2

Related Questions