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

为君一笑 提交于 2021-01-20 19:49:25

问题


I have two pandas dataframes

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.


回答1:


from 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



回答2:


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


来源:https://stackoverflow.com/questions/36725720/merging-two-dataframes-with-same-column-names-but-different-number-of-columns-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!