Merging two dataframes with same column names but different number of columns in pandas
31,307
Solution 1
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
Solution 2
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
Author by
Falconic
Updated on July 09, 2022Comments
-
Falconic almost 2 years
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.
-
Falconic about 8 yearsThanks this does the trick. I am marking this as answer because it also caters to empty dataframes. Append method doesn't work with empty dataframes.