问题
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