Pandas update column values from another dataframe

The pandas library in Python provides various functions and methods to update column values from another dataframe. Here is an example:

Let’s say we have two dataframes:

Dataframe 1 Dataframe 2

“`python
import pandas as pd

dataframe1 = pd.DataFrame({
‘ID’: [‘1’, ‘2’, ‘3’],
‘Name’: [‘John’, ‘Emma’, ‘Mike’]
})
“`

“`python
dataframe2 = pd.DataFrame({
‘ID’: [‘2’, ‘3’],
‘Age’: [’25’, ’30’]
})
“`

Dataframe 1:

ID Name
1 John
2 Emma
3 Mike

Dataframe 2:

ID Age
2 25
3 30

To update the ‘Age’ column in Dataframe 1 using values from Dataframe 2 based on matching ‘ID’, you can use the merge function along with the update method.

“`python
dataframe1 = dataframe1.merge(dataframe2, on=’ID’, how=’left’)
dataframe1.update(dataframe1[[‘Age’]].fillna(”))
“`

After executing the above code snippet, the updated Dataframe 1 would be:

ID Name Age
1 John
2 Emma 25
3 Mike 30

In the above code snippet, we first merge Dataframe 1 and Dataframe 2 using the ‘ID’ column as the key. This performs a left join, keeping all rows from Dataframe 1 even if there is no matching ‘ID’ in Dataframe 2.

Then, we use the update method to update the ‘Age’ column in Dataframe 1 with the corresponding values from Dataframe 2. The fillna('') function is used to replace missing values with empty strings.

Finally, the updated Dataframe 1 is displayed with ‘ID’, ‘Name’, and ‘Age’ columns.

Leave a comment