Pandas merge two dataframes with different columns

前端 未结 2 1241
我寻月下人不归
我寻月下人不归 2020-12-02 10:32

I\'m surely missing something simple here. Trying to merge two dataframes in pandas that have mostly the same column names, but the right dataframe has some columns that the

2条回答
  •  孤城傲影
    2020-12-02 11:01

    I think in this case concat is what you want:

    In [12]:
    
    pd.concat([df,df1], axis=0, ignore_index=True)
    Out[12]:
       attr_1  attr_2  attr_3  id  quantity
    0       0       1     NaN   1        20
    1       1       1     NaN   2        23
    2       1       1     NaN   3        19
    3       0       0     NaN   4        19
    4       1     NaN       0   5         8
    5       0     NaN       1   6        13
    6       1     NaN       1   7        20
    7       1     NaN       1   8        25
    

    by passing axis=0 here you are stacking the df's on top of each other which I believe is what you want then producing NaN value where they are absent from their respective dfs.

提交回复
热议问题