Pandas:如何连接具有不同列的数据框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44252759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: How to concatenate dataframes with different columns?
提问by gmolau
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:
我试图在官方Pandas 文档中找到答案,但发现它比帮助更令人困惑。基本上我有两个具有重叠但不相同的列列表的数据框:
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?
应该相当简单,但我尝试了几种直观的方法,但总是出错。有谁能够帮助我?
采纳答案by Vaishali
You need merge with parameter how = outer
您需要与参数 how = 外部合并
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
回答by Scott Boston
If you just want to concatenate the dataframes you can use.
如果您只想连接可以使用的数据帧。
pd.concat([df1,df2])
output:
输出:
A B C
0 22.0 34 NaN
1 78.0 42 NaN
0 NaN 76 11.0
1 NaN 11 67.0
Then you can reset_index to recreate a simple incrementing index.
然后你可以 reset_index 重新创建一个简单的递增索引。
pd.concat([df,df2]).reset_index(drop = True)
Output:
输出:
A B C
0 22.0 34 NaN
1 78.0 42 NaN
2 NaN 76 11.0
3 NaN 11 67.0