Concatenate distinct columns in two dataframes using pandas (and append similar columns)

后端 未结 4 1186
眼角桃花
眼角桃花 2020-12-17 05:29

My question is closely related to Pandas Merge - How to avoid duplicating columns but not identical.

I want to concatenate the columns that are diff

4条回答
  •  时光取名叫无心
    2020-12-17 05:57

    You can extract only those columns from df2 (and df3 similarly) which are not already present in df1. Then just use pd.concat to concatenate the data frames:

    cols = [c for c in df2.columns if c not in df1.columns]
    df = pd.concat([df1, df2[cols]], axis=1)
    

提交回复
热议问题