Python Pandas merge only certain columns

后端 未结 5 576
小蘑菇
小蘑菇 2020-12-12 12:33

Is it possible to only merge some columns? I have a DataFrame df1 with columns x, y, z, and df2 with columns x, a ,b, c, d, e, f, etc.

I want to merge the two DataFr

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 13:19

    This is to merge selected columns from two tables.

    If table_1 contains t1_a,t1_b,t1_c..,id,..t1_z columns, and table_2 contains t2_a, t2_b, t2_c..., id,..t2_z columns, and only t1_a, id, t2_a are required in the final table, then

    mergedCSV = table_1[['t1_a','id']].merge(table_2[['t2_a','id']], on = 'id',how = 'left')
    # save resulting output file    
    mergedCSV.to_csv('output.csv',index = False)
    

提交回复
热议问题