简体   繁体   English

如何按列连接两个数据框?

[英]How to join two dataframes columnwise?

I have two dataframes df1 and df2 containing an equal number of columns.我有两个数据df1df2包含相同数量的列。 I want to create another dataframe df by joining df1 and df2 .我想通过加入df1df2创建另一个 dataframe df

The resulting dataframe should be formed by selecting column1 from df1 and column1 from df2 and join them together.生成的 dataframe 应通过从df1中选择column1和从df2中选择column1并将它们连接在一起来形成。 Similarly column2 from df1 and column2 from df2 and so on.类似地,来自df1column2和来自df2column2等等。

Try this:尝试这个:

You can replace 'Column 1' and 'Column 2' with whatever name is required in your context.您可以将“Column 1”和“Column 2”替换为上下文中所需的任何名称。

import pandas as pd

df = pd.merge(df1, df2, on=['Column 1','Column 2'], how = 'outer')

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

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