简体   繁体   English

Pandas 连接两个数据帧,两个数据帧中具有相同索引的列彼此相邻

[英]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.令人惊讶的是,我没有找到任何关于此的文档,也没有在 StackOverflow 上找到任何现有问题。

So it might be easier to describe my question in an example:所以在一个例子中描述我的问题可能更容易:

df1: df1:

A一种 B C C
5 5 3 3 7 7
6 6 8 8 9 9

df2: df2:

One Two Three
5 5 3 3 7 7
6 6 8 8 9 9

Expected Result预期结果

A一种 One B Two C C Three
5 5 3 3 7 7 5 5 3 3 7 7
6 6 8 8 9 9 6 6 8 8 9 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]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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