221

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 DataFrames on x, but I only want to merge columns df2.a, df2.b - not the entire DataFrame.

The result would be a DataFrame with x, y, z, a, b.

I could merge then delete the unwanted columns, but it seems like there is a better method.

Ajean
  • 5,528
  • 14
  • 46
  • 69
BubbleGuppies
  • 5,750
  • 7
  • 20
  • 15

6 Answers6

277

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.

Arthur D. Howland
  • 4,363
  • 3
  • 21
  • 31
108

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

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

df1.merge(df2[list('xab')])
beroe
  • 11,784
  • 5
  • 34
  • 79
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • 7
    Hmmm, I wonder if there should be a native way to do this, like subset in dropna... will put together github issue – Andy Hayden Jul 31 '13 at 19:12
  • Hmmm ... I tried using this to merge column 'Unique_External_Users' from df2 to df1 but got an error ... "None of [Index(['U', 'n', 'i', 'q', 'u', 'e', '_', 'E', 'x', 't', 'e', 'r', 'n', 'a',\n 'l', '_', 'U', 's', 'e', 'r', 's'],\n dtype='object')] are in the [columns]" . – CoolDocMan Feb 28 '20 at 20:48
  • Here's the code . ... df1.merge(df2('Unique_External_Users')]) – CoolDocMan Feb 28 '20 at 20:53
  • 7
    @CoolDocMan I think you missed something from the proposed answer: `list('xab')` takes each element (letter) of the string 'xab' and converts it to a list element so `list('xab')` returns `['x', 'a', 'b']`. That works if each column has a single letter as a name. In your case I think you need to do df1.merge(df2['Unique_External_Users'], *other_arguments). ...Most probably you already solved it by now, just leaving this for newbies around, like me – SOf_PUAR Jul 03 '20 at 07:11
46

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(columns = 'key1') part will prevent 'key1' from being kept in the resulting data frame, despite it being required to join in the first place.

tonneofash
  • 649
  • 6
  • 13
12

You can use .loc to select the specific columns with all rows and then pull that. An example is below:

pandas.merge(dataframe1, dataframe2.iloc[:, [0:5]], how='left', on='key')

In this example, you are merging dataframe1 and dataframe2. You have chosen to do an outer left join on 'key'. However, for dataframe2 you have specified .iloc which allows you to specific the rows and columns you want in a numerical format. Using :, your selecting all rows, but [0:5] selects the first 5 columns. You could use .loc to specify by name, but if your dealing with long column names, then .iloc may be better.

Ajean
  • 5,528
  • 14
  • 46
  • 69
  • 3
    Beware that [`.loc` will make a copy](https://stackoverflow.com/questions/23296282/what-rules-does-pandas-use-to-generate-a-view-vs-a-copy), and on a large df that can be painful. It might be better to merge then immediately take a column slice in the same expression. – smci Apr 19 '18 at 06:33
9

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)
nick
  • 1,090
  • 1
  • 11
  • 24
Marco167
  • 371
  • 3
  • 7
3

Slight extension of the accepted answer for multi-character column names, using inner join by default:

df1 = df1.merge(df2[["Key_Column", "Target_Column1", "Target_Column2"]])

This assumes that Key_Column is the only column both dataframes have in common.

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55