overriding dataframe row value with another dataframe

Viewed 9

df1:

|name     |favorite animal|
|---------|---------------|
|Mary     |cat            |
|Benny    |shark          |
|Jack     |dog            |
|Becca    |sheep          |
|Christie |dinosaur       |

df2:

|name     |favorite animal|
|---------|---------------|
|Mary     |dog            |
|Benny    |fish           |
|Christie |monkey         |  

Is there anyway I can replace the value in df1 by value in df2 by matching on name between df1 and df2 (names that appear in df2 will update "favorite animal on df1) so that the output looks like

|name     |favorite animal|
|---------|---------------|
|Mary     |dog            |
|Benny    |fish           |
|Jack     |dog            |
|Becca    |sheep          |
|Christie |monkey         |  

Thanks

1 Answers

Create a zip list of name and favorite animal for df2 and use replace:

lst = list(zip(df2['name'], df2['favorite animal']))
df1['favorite animal'] = df1['favorite animal'].replace(lst)
Related