简体   繁体   English

合并两个具有相同列名但在pandas中具有不同列数的数据帧

[英]Merging two dataframes with same column names but different number of columns in pandas

I have two pandas dataframes 我有两个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

I have tried using append and concat but nothing seems to work. 我尝试过使用append和concat,但似乎没有任何效果。 Any help would be much appreciated. 任何帮助将非常感激。

Concatenate the dataframes 连接数据帧

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

from doc-ref ref try: df1.append(df2, ignore_index=True) 来自doc-ref ref try: df1.append(df2, ignore_index=True)

sample output: 样本输出:

    A     B     C
 0  0   123   321
 1  0  1543   432
 2  1   NaN   124
 3  1   NaN  1544

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM