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

To check if few values in dataframe column exists in another dataframe column

I am trying to check if values present in df1 column is present in df2 column.
df2 contains more values than df1 and cant use for loop

import pandas as pd

df1 = pd.DataFrame({'one': [2,4,6,8]})
df2 = pd.DataFrame({'one': [4,2,6,8,10]})

print(df1.isin(df2))  

expected results

    one
0  True
1  True
2   True
3   True

actual results

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

     one
0  False
1  False
2   True
3   True

2 and 4 are present in df2 , still due to order false is given. is there a way where order of values don’t impact

>Solution :

You can compare columns:

print(df1['one'].isin(df2['one']))  
0    True
1    True
2    True
3    True
Name: one, dtype: bool

Or convert values of DataFrame to 1d array and then list:

print(df1.isin(df2.to_numpy().ravel().tolist()))  
    one
0  True
1  True
2  True
3  True
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