在 Pandas 中合并列名相同但列数不同的两个数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36725720/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:04:43  来源:igfitidea点击:

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

pythonpandasdataframemergeappend

提问by Falconic

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. Any help would be much appreciated.

我曾尝试使用 append 和 concat 但似乎没有任何效果。任何帮助将非常感激。

回答by Joshua Baboo

from doc-refref try: df1.append(df2, ignore_index=True)

doc-refref 尝试: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

回答by Abbas

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