Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python -How to compare columns from two dataframe and create 3rd with new values?

I have two dataframes that contains names. What I am need to do is to check which of the names in second dataframe are not present in the first dataframe.
For this example

list1 = ['Mark','Sofi','Joh','Leo','Jason']
df1 = pd.DataFrame(list1, columns =['Names'])

and

list2 = ['Mark','Sofi','David','Matt','Jason']
df2 = pd.DataFrame(list2, columns =['Names'])

So basically I in this simple example we can see that David and Matt from second dataframe do not exist in the first dataframe.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need programmatically to come up with 3rd dataframe that will have results like this:

Names
David
Matt

My first thought was to try using pandas merge function but I am unable to get the unique set of names from df2 that are not in df1.

Any thoughts on how to do this?

>Solution :

You can create the 3rd dataframe filtering the 2nd with a condition like this..

df3 = df2[~df2['Names'].isin(df1['Names'])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading