Python Pandas merge only certain columns Python Pandas merge only certain columns python python

Python Pandas merge only certain columns


You want to use TWO brackets, so if you are doing a VLOOKUP sort of action:

df = pd.merge(df,df2[['Key_Column','Target_Column']],on='Key_Column', how='left')

This will give you everything in the original df + add that one corresponding column in df2 that you want to join.


You could merge the sub-DataFrame (with just those columns):

df2[list('xab')]  # df2 but only with columns x, a, and bdf1.merge(df2[list('xab')])


If you want to drop column(s) from the target data frame, but the column(s) are required for the join, you can do the following:

df1 = df1.merge(df2[['a', 'b', 'key1']], how = 'left',                left_on = 'key2', right_on = 'key1').drop(columns= ['key1'])

The .drop('key1') part will prevent 'key1' from being kept in the resulting data frame, despite it being required to join in the first place.