I have been trying to compare two dataframes to find missing rows and different rows:
Case 1: Same number of rows and different rows:
In this case, I have same number of rows, but two different rows:
dict_a = {'Values 1':[15, 2, 3, 24, 5, 16], 'Values 2':[10, 7, 3, 5, 6, 23],
'Values 3': ["Apple", "Orange", "Kiwi", "Cherry", "Banana", "Grapes"]}
dict_b = {'Values 1':[15, 3, 3, 24, 5, 16], 'Values 2':[10, 7, 3, 5, 6, 23],
'Values 3': ["Apple", "Orange", "Kiwi", "Cherry", "Banana", "Grape"]}
df1 = pd.DataFrame(dict_a)
df2 = pd.DataFrame(dict_b)
So, I can find the different row indexes using:
list(df1[~df1.isin(df2)].dropna(how = 'all').index)
This results in [1, 5].
Case 2: Different number of rows and different rows:
In this case, I have different number of rows, and two different rows.
dict_a = {'Values 1':[15, 2, 3, 24, 1], 'Values 2':[10, 7, 3, 5, 6],
'Values 3': ["Apple", "Orange", "Kiwi", "Cherry", "Banana"]}
dict_b = {'Values 1':[15, 1, 3, 24, 5, 16], 'Values 2':[10, 7, 3, 5, 6, 23],
'Values 3': ["Apple", "Orange", "Kiwi", "Cherry", "Banana", "Grape"]}
df1 = pd.DataFrame(dict_a)
df2 = pd.DataFrame(dict_b)
display(df1)
display(df2)
Here, first I check for missing rows. From this, I can find that it is the fifth index by comparing the dataframe with greater length with shorter length:
df2[~df2.index.isin(df1.index)]
I can also find the different rows:
df1[~df1.isin(df2)].dropna(how = 'all')
which are [1, 4].
Case 3: Interchanged rows, but same row data
dict_a = {'Values 1':[2, 15, 3, 24, 5, 16], 'Values 2':[7, 10, 3, 5, 6, 23],
'Values 3': ["Orange", "Apple", "Kiwi", "Cherry", "Banana", "Grape"]}
dict_b = {'Values 1':[15, 2, 3, 24, 5, 16], 'Values 2':[10, 7, 3, 5, 6, 23],
'Values 3': ["Apple", "Orange", "Kiwi", "Cherry", "Banana", "Grape"]}
df1 = pd.DataFrame(dict_a)
df2 = pd.DataFrame(dict_b)
display(df1)
display(df2)
However, the code doesn't work for interchanged rows. It needs to check if row already exists regardless of position and return a result "there are no different rows", but instead it says rows at index [0, 1] are different.
So, I am looking for a solution which can compare the two dataframes regardless of row order or position, missing rows, and different rows.
So, my goal is to compare two dataframes and return "missing rows" and also "different rows" i.e. which don't completely match each other. Is there a such a function which can actually compare the rows in this way?




