熊猫:水平组合两个数据框[重复]

时间:2023-01-05 22:57:37

This question already has an answer here:

这个问题在这里已有答案:

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 DataFrame,每个都有不同的列。我想基本上将它们水平粘合在一起(它们每个都有相同的行数,所以这不应该是一个问题)。

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

必须有一个简单的方法来做这个,但我已经完成了文档和concat不是我正在寻找的(我不认为)。

Any ideas?

Thanks!

1 个解决方案

#1


6  

concat is 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 pddf1 = 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)

#1


6  

concat is 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 pddf1 = 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)