idontreallyknowhehe
idontreallyknowhehe

Reputation: 113

Pandas join two dataframes, columns with the same index from the two dataframes are next to each other

I'm trying to join two dataframes together. They don't have any column with the same name, and I would like to make the columns with the same index in each dataframe to be put next to each other. Surprisingly, I did not find any document about this, or any existing question on StackOverflow.

So it might be easier to describe my question in an example:

df1:

A B C
5 3 7
6 8 9

df2:

One Two Three
5 3 7
6 8 9

Expected Result

A One B Two C Three
5 3 7 5 3 7
6 8 9 6 8 9

Does anyone have an idea how to make this happen?

Upvotes: 0

Views: 114

Answers (1)

rhug123
rhug123

Reputation: 8768

Here is another way:

(pd.DataFrame(
    pd.concat([df,df2],axis=1)
    .to_numpy(),
    columns = [j for i in zip(df.columns,df2.columns) for j in i]))

Upvotes: 1

Related Questions