pandas :如何连接具有不同列的数据框? [英] Pandas: How to concatenate dataframes with different columns?

查看:56
本文介绍了 pandas :如何连接具有不同列的数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在官方熊猫文档中找到答案,但发现它比帮助更令人困惑.基本上,我有两个重叠但不相同的列列表的数据框:

I tried to find the answer in the official Pandas documentation, but found it more confusing than helpful. Basically I have two dataframes with overlapping, but not identical column lists:

df1:
   A   B
0  22  34
1  78  42

df2:
   B   C
0  76  29
1  11  67

我要合并/合并/附加它们,以便结果为

I want to merge/concatenate/append them so that the result is

df3:
   A   B   C
0  22  34  nan
1  78  42  nan
2  nan 76  29
3  nan 11  67

应该很简单,但是我尝试了几种直观的方法并且总是会出错.有人可以帮我吗?

Should be fairly simple, but I've tried several intuitive approaches and always got errors. Can anybody help me?

推荐答案

您需要与参数how = external合并

You need merge with parameter how = outer

df3 = df1.merge(df2, how = 'outer')

    A       B   C
0   22.0    34  NaN
1   78.0    42  NaN
2   NaN     76  29.0
3   NaN     11  67.0

这篇关于 pandas :如何连接具有不同列的数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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