【问题标题】:Pandas join two dataframes, columns with the same index from the two dataframes are next to each otherPandas 加入两个数据帧,两个数据帧中具有相同索引的列彼此相邻
【发布时间】:2021-06-30 22:25:53
【问题描述】:

我正在尝试将两个数据框连接在一起。他们没有任何同名的列,我想让每个数据框中具有相同索引的列彼此相邻。令人惊讶的是,我没有在 StackOverflow 上找到任何关于此的文档或任何现有问题。

所以用一个例子来描述我的问题可能更容易:

df1:

A B C
5 3 7
6 8 9

df2:

One Two Three
5 3 7
6 8 9

预期结果

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

有人知道如何实现吗?

【问题讨论】:

    标签: python pandas dataframe join


    【解决方案1】:

    这是另一种方式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多