Pandas merge two dataframes with different columns

前端 未结 2 1106
我寻月下人不归
我寻月下人不归 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:00

    I had this problem today using any of concat, append or merge, and I got around it by adding a helper column sequentially numbered and then doing an outer join

    helper=1
    for i in df1.index:
        df1.loc[i,'helper']=helper
        helper=helper+1
    for i in df2.index:
        df2.loc[i,'helper']=helper
        helper=helper+1
    df1.merge(df2,on='helper',how='outer')
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题