1

df1:

col1  col2   col3 
abc   123    @#   
def   456    *&   
pqr   789    ^%   

df2:

col1  col2   col3 
def   456    *&   
pqr   789    ^%   
abc   123    @#   
oup   852    $%   

I have to check df1['col1'] with df2['col1'], as all the values of df1['col1'] are present in df2['col1'] it should return 'True'.

eg: to check def of df1['col1'] with def of df2['col1'] & likewise

I have used isin,eq,issubset functions which is not working

Expected output: df2:

col1  col1_filter    col2   col2_filter  col3  Col3_filter
def     True          456      True        *&   True
pqr                   789                  ^%   
abc                   123                   @#   
oup                   852                   $%   
Sijin John
  • 492
  • 7
  • 15
  • 1
    what is your expected output? – Sociopath Jan 29 '20 at 11:09
  • This article on [Comparing rows across Dataframes](https://hackersandslackers.com/compare-rows-pandas-dataframes/) could help. – DarrylG Jan 29 '20 at 11:13
  • Is it checking each row or a column. From the example given, itseems you want check if values of one column of a dataframe is present in a column of other dataframe – Srikar Manthatti Jan 29 '20 at 11:16
  • `df2['col1'].isin(df1['col1'])` should work, it will return `True` for all the values of `df2` which are in `df1`. – Erfan Jan 29 '20 at 11:21
  • First we have to concatenate two tables(dfs).Then take headers of each df separately in 2 separate list. Then use 'df_validation[(list2[i])] = df1[list1[i]].str.strip().isin(df2[list2[i]].str.strip().tolist())' – Sijin John Feb 05 '20 at 06:43

2 Answers2

0
import pandas as pd
df = pd.DataFrame()
df['col1']= ['abc','bcd','def']
df['col2']= ['5','6','7']
df1 = pd.DataFrame()
df1['col1']= ['asa','bcd','sa']
df1['col2']= ['8','6','9']

df = df[df==df1] #main code to compare the rows
print(df.head())

tried to implement the same thing with my code , this solution can help you

manoj yadav
  • 347
  • 2
  • 7
0

isin works fine for my machine as

df1.col1.isin(df2.col1)

This returns boolean True, if the row of df1 exists in df2. You can also merge the 2 dataframes and then compare.

Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18
  • Its giving 'False' as the col values are shuffled,& also can 'isin' work to check to traverse 1 value from the column (like 'DSL') to another col of df? – Sijin John Jan 29 '20 at 11:37
  • I did not understand what do you want to say. Please be elaborate. Also, do you want to check for all columns separately? Like, if col1 and col3 exist in df2, but col2 doesn't, then it should return True False True? – Swati Srivastava Jan 29 '20 at 11:40
  • yes...I want True or False value for each col. – Sijin John Jan 29 '20 at 11:43
  • You can add a column to both dataframes, named ID, containing row number. Merge the dataframes now, and do l1 = pd.Series(df.col11.isin(df.col21)) where col11 is col1 of df1 and col21 is col1 of df2. In the next line, do df['Same1'] = l1. This will add a column in the merged dataframe, showing True or False, based on if col11 value exists in col21 – Swati Srivastava Jan 29 '20 at 11:58
  • You can do similar for the rest of the columns – Swati Srivastava Jan 29 '20 at 11:58
  • Its giving 'False' as the values are shuffled. eg: if index 1 of col1 is abc then in other col2 index 4 is 'abc' and so on....thatz why isin is not working – Sijin John Jan 29 '20 at 12:50
  • This is where isin() function comes handy. I tried my code with the exact dataframe you posted. isin() works perfectly fine for me. – Swati Srivastava Jan 30 '20 at 05:40