首页 > 解决方案 > 熊猫如何在没有键的情况下组合两个表

问题描述

我想要的很简单,但是pandas对我来说很困惑,我只想合并两个表

表格1:

Column 1,Column 2

Value1, Value1

Value2, Value2

表 2:

Column 3,Column 4

Value1, Value1

Value2, Value2

我想要的是

Column 1,Column 2,Column 3,Column 4
Value1, Value1, Value1, Value1
Value2, Value2, Value2, Value2

注意:没有相同的列,没有键,只是逐行组合,所以我认为“合并”不起作用

标签: pythonpython-3.xpandas

解决方案


假设您有以下数据框/表:

df1 = pd.DataFrame.from_dict({'a': [1,2,3], 'b': [4,5,6]})

   a  b
0  1  4
1  2  5
2  3  6

df2 = pd.DataFrame.from_dict({'c': [7,8,9], 'd': [10,11,11]})

   c   d
0  7  10
1  8  11
2  9  11

使用以下代码行,您可以连接它们:

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

输出:

   a  b  c   d
0  1  4  7  10
1  2  5  8  11
2  3  6  9  11

推荐阅读