TW1411 TW1411 - 3 months ago 14
Python Question

Pandas add column to df based on values in a second df

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?

Answer
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