Python Pandas:水平组合两个数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44723377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:24:06  来源:igfitidea点击:

Pandas: Combining Two DataFrames Horizontally

pythonpandasdataframe

提问by bclayman

I have two Pandas DataFrames, each with different columns. I want to basically glue them together horizontally (they each have the same number of rows so this shouldn't be an issue).

我有两个 Pandas DataFrames,每个都有不同的列。我想基本上将它们水平粘合在一起(它们每个都有相同的行数,所以这应该不是问题)。

There must be a simple way of doing this but I've gone through the docs and concatisn't what I'm looking for (I don't think).

必须有一种简单的方法来做到这一点,但我已经阅读了文档,concat这不是我想要的(我不认为)。

Any ideas?

有任何想法吗?

Thanks!

谢谢!

回答by nslamberth

concatis indeed what you're looking for, you just have to pass it a different value for the "axis" argument than the default. Code sample below:

concat确实是您正在寻找的,您只需为“轴”参数传递一个与默认值不同的值。下面的代码示例:

import pandas as pd

df1 = pd.DataFrame({
    'A': [1,2,3,4,5],
    'B': [1,2,3,4,5]
})

df2 = pd.DataFrame({
    'C': [1,2,3,4,5],
    'D': [1,2,3,4,5]
})

df_concat = pd.concat([df1, df2], axis=1)

print(df_concat)