Home > database >  To check if few values in dataframe column exists in another dataframe column
To check if few values in dataframe column exists in another dataframe column

Time:06-09

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

     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

CodePudding user response:

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
  • Related