Jim
Jim

Reputation: 35

Python Merge Two DataFrames Only Retrieve Specific Columns in the Result

Two DataFrames

Hi - I want to merge two python DataFrames, but don't want to bring over ALL of the columns from both dataframes to my new dataframe. In the picture below, if I join df1 and df2 on 'acct' and want to bring back all the columns from df1 and ONLY 'entity' from df2, how would I write that? I don't want to have to drop any columns so doing a normal merge isn't what I'm looking for. Can anyone help? Thanks!

Upvotes: 0

Views: 744

Answers (1)

Anderson Monken
Anderson Monken

Reputation: 66

When you perform the merge operation, you can modify a dataframe object that is in your function, which will mean the underlying objects df1 and df2 remain unchanged. An example would look like this:

df_result = df1.merge(df2[ ['acct','entity'] ], on ='acct')

This will let you do your partial merge without modifying either original dataframe.

Upvotes: 1

Related Questions