3

I have two separate dataframes df1 and df2, both dataframes contain an id column which links rows between them. df2 has a group column that df1 does not contain. What I would like to do is go through each id in df1 and check to see if it is in df2 then if it is to take the group column value and put it in df1 under a new column of the same name. Would it be easiest to write a function to loop through or is there a pandas trick I could utilize here?

1
  • 1
    You may be looking for merge. Eg. pandas.merge(df1, df2, how='outer', on=['id']) Commented Aug 11, 2016 at 16:29

2 Answers 2

3
df1 = pd.DataFrame([[1, 'a'],
                    [2, 'b'],
                    [3, 'c']], columns=['id', 'attr'])
df2 = pd.DataFrame([[2, 'd'],
                    [3, 'e'],
                    [4, 'f']], columns=['id', 'group'])

df1.merge(df2, how='left')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

3

You can merge the two dataframes in one by joining them on the id column and then keep only the columns that you need:

df1 = merge(df1, df2, how='left', on='id')
df1.drop('unwanted_column',1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.