简体   繁体   中英

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?

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]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM