如何使用pandas DataFrame在列式轴连接中使用join_axes? [英] How to use join_axes in the column-wise axis concatenation using pandas DataFrame?

查看:1142
本文介绍了如何使用pandas DataFrame在列式轴连接中使用join_axes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据集:

df2 = pd.DataFrame({'A':[1, 2], 'B':[3, 4]})
df3 = pd.DataFrame({'A':[5, 6, 7], 'B':[8, 9, 10], 'C':[11, 12, 13]})

axis=1时,我只能在行"级联中使用join_axes,如下所示:

I am only able to use join_axes in the 'row-wise' concatenation when axis=1 as shown below:

pd.concat([df2, df3], axis=1, join_axes = [df2.index])

以上内容将产生:

但是,相对于axis=0尝试此操作会产生AttributeError: 'NoneType' object has no attribute 'is_extension'错误:

However, trying this relative to axis=0 will yield an error of AttributeError: 'NoneType' object has no attribute 'is_extension':

pd.concat([df2, df3], axis=0, join_axes=[df2.index])

我也尝试过手动创建列表,但AttributeError: 'str' object has no attribute 'equals'均不起作用:

I have also tried making a list manually and that doesn't work either AttributeError: 'str' object has no attribute 'equals':

pd.concat([df2, df3], axis=0, join_axes=['A'])

预期的输出是(从下面的答案中获得):

Expected output is (obtained from answer below):

推荐答案

我认为需要通过df2的列设置join_axes:

I believe need set join_axes by columns of df2:

df = pd.concat([df2, df3], join_axes = [df2.columns])
print (df)
   A   B
0  1   3
1  2   4
0  5   8
1  6   9
2  7  10

这篇关于如何使用pandas DataFrame在列式轴连接中使用join_axes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆