3

I am trying to add a column to a pandas dataframe (df1) that has a unique identifier ('id') column from another dataframe (df2) that has the same unique identifier ('sameid'). I have tried merge, but I need to only add one specific column ('addthiscolumn') not all of the columns. What is the best way to do this?

print df1 'id' 'column1' 0 aaa randomdata1 1 aab randomdata2 2 aac randomdata3 3 aad randomdata4 print df2 'sameid' 'irrelevant' 'addthiscolumn' 0 aaa irre1 1234 1 aab irre2 2345 2 aac irre3 3456 3 aad irre4 4567 4 aae irre5 5678 5 aad irre6 6789 

Desired Result

print df1 'id' 'column1' 'addthiscolumn' 0 aaa randomdata1 1234 1 aab randomdata2 2345 2 aac randomdata3 3456 3 aad randomdata4 4567 
1
  • 3
    try df1.merge(df2, left_on='id', right_on='sameid') Commented Dec 21, 2015 at 21:50

1 Answer 1

3

Because you just want to merge a single column, you can select as follows:

df1.merge(df2[['sameid', 'addthiscolumn']], left_on='id', right_on='sameid') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.