2
votes

How do I write a function that checks two input dataframes are of equal as long as rows in both dataframes are equal? So it disregards index positions and column orders. I can't use df.equals() since it will enforce data types to be equal, which is not what I need.

from io import StringIO

canonical_in_csv = """,c,a,b
2,hat,x,1
0,rat,y,4
3,cat,x,2
1,bat,x,2"""

with StringIO(canonical_in_csv) as fp:
    df1 = pd.read_csv(fp, index_col=0)

canonical_soln_csv = """,a,b,c
0,x,1,hat
1,x,2,bat
2,x,2,cat
3,y,4,rat"""

with StringIO(canonical_soln_csv) as fp:
    df2 = pd.read_csv(fp, index_col=0)

df1:

    c   a   b
2   hat x   1
0   rat y   4
3   cat x   2
1   bat x   2

df2:

    a   b   c
0   x   1   hat
1   x   2   bat
2   x   2   cat
3   y   4   rat

My attempt:

temp1 = (df == df2).all()
temp2 = temp1.all()
temp2

ValueError: Can only compare identically-labeled DataFrame objects

3
Why do you want this kind of equality test? May I know the motive - Bharath
going through set of practice problems for an online python course - Si_CPyR

3 Answers

1
votes

You can use sort_index by index and columns values first, then merge with eq (==) or equals:

df11 = df1.sort_index().sort_index(axis=1)
df22 = df2.sort_index().sort_index(axis=1)

print (df11.merge(df22))
   a  b    c
0  y  4  rat
1  x  2  bat
2  x  1  hat
3  x  2  cat

print (df11.merge(df22).eq(df11))
      a     b     c
0  True  True  True
1  True  True  True
2  True  True  True
3  True  True  True

a = df11.merge(df22).eq(df11).values.all()
#alternative
#a = df11.merge(df22).equals(df11)
print (a)
True

Your function should be rewritten:

def checkequality(A, B):

    df11 = A.sort_index(axis=1)
    df11 = df11.sort_values(df11.columns.tolist()).reset_index(drop=True)

    df22 = B.sort_index(axis=1)
    df22 = df22.sort_values(df22.columns.tolist()).reset_index(drop=True)
    return (df11 == df22).values.all()

a = checkequality(df1, df2)
print (a)
True
1
votes

You request on row index dis-regard is pretty difficult to undertake as this datatype is not optimized for such operation whereas regarding columns issue, fortunately this will help you

df1.values == df2[df1.columns].values

where df1.columns syncs the columns order and values convert to numpy for comparison. I still recommend not doing row re-ordering and match as that can be very taxing for bigger dataset. Based on index match this can be what you are looking for

df1.values==df2.reindex(df1.index.values.tolist())[df1.columns].values

Update

As pointed by @Dark a cleaner and in-place comparison can be done like this

df1.loc[df2.index,df2.columns] == df2
0
votes

I figured it out,

def checkequality(A, B):

var_names = sorted(A.columns)
var_names
Y = A[var_names].copy()
Y.sort_values(by = var_names,inplace=True)
Y.set_index([list(range(0,len(Y)))],inplace=True)

var_names2 = sorted(B.columns)
var_names2
Y2 = B[var_names2].copy()
Y2.sort_values(by = var_names2,inplace=True)
Y2.set_index([list(range(0,len(Y2)))],inplace=True)

if (Y==Y2).all().all() == True:
    return True
else:
    return False