在 pandas 中合并具有相同列名但列数不同的两个数据框 [英] Merging two dataframes with same column names but different number of columns in pandas
本文介绍了在 pandas 中合并具有相同列名但列数不同的两个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个熊猫数据框
df1 = DataFrame([[0,123,321],[0,1543,432]], columns=['A', 'B','C'])
df2 = DataFrame([[1,124],[1,1544]], columns=['A', 'C'])
我想将它们合并,以便新数据框如下所示
I want to merge these so that the new dataframe would look like below
A | B | C
0 123 321
0 1543 432
1 null 124
1 null 1544
我尝试使用append和concat,但是似乎没有任何效果.任何帮助将不胜感激.
I have tried using append and concat but nothing seems to work. Any help would be much appreciated.
推荐答案
连接数据框
import pandas as pd
pd.concat([df1,df2], axis=0)
A B C
0 0 123 321
1 0 1543 432
0 1 NaN 124
1 1 NaN 1544
这篇关于在 pandas 中合并具有相同列名但列数不同的两个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文