Skip to content
Advertisement

Check if values in one dataframe match values from another, updating dataframe

Let’s say I have 2 dataframes, both have different lengths but the same amount of columns

JavaScript

Lets assume that some of the data in df1 is outdated and I’ve received a new dataframe that contains some new data but not which may or may not exist already in the outdated dataframe.

I want to find out if any of the values of df2.country are inside df1.country

By doing the following I’m able to return a boolean:

JavaScript

Unfortunately I’m just creating a new dataframe containing the answer to my question

JavaScript

My goal here is to delete the rows of df1 which values match with df2 and add the new data, kind of like an update.

I’ve manage to come up with something like this:

JavaScript

which in fact works and updates the dataframe

JavaScript

But I really believe there’s a batter way of doing the same thing quicker and much more practical considering that the real dataframe is much bigger and updates every few seconds.

I’d love to hear some suggestions, Thanks!

Advertisement

Answer

The isin approach is so close! Simply use the results from isin as a mask, then concat the rows from df1 that are not in (~) df2 with the rest of df2:

JavaScript

df3:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement