Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用字符串逐列连接两个dfs_Python_Pandas - Fatal编程技术网

Python 用字符串逐列连接两个dfs

Python 用字符串逐列连接两个dfs,python,pandas,Python,Pandas,我有两个表df1和df2 print(df1) bar1 foo1 0 a e 1 b f 2 c g print(df2) bar2 foo2 0 x l 1 y m 2 z n 我需要将它们逐列连接到一个包含两列的表中 我需要这张桌子: bar3 foo3 0 a x e l 1 b y f m 2 c z g n 如何使用pandas进行操作?您只需使用所需的分隔符添

我有两个表
df1
df2

print(df1)
    bar1 foo1
0    a   e
1    b   f
2    c   g

print(df2)
    bar2 foo2
0    x   l
1    y   m
2    z   n
我需要将它们逐列连接到一个包含两列的表中

我需要这张桌子:

     bar3  foo3
0    a x   e l
1    b y   f m
2    c z   g n

如何使用pandas进行操作?

您只需使用所需的分隔符添加两个数据帧

new_df = df1 + ' ' + df2

    bar   foo
0   a x   e l
1   b y   f m
2   c z   g n
如果两个DataFrame的列名不匹配,请添加底层数组并使用所需的列名调用DataFrame构造函数

pd.DataFrame(df1.values + ' ' + df2.values, columns = ['bar3', 'foo3'])

    bar3    foo3
0   a x     e l
1   b y     f m
2   c z     g n