As part of a unit test, I need to test two DataFrames for equality. The order of the columns in the DataFrames is not important to me. However, it seems to matter to Panda
Usually you're going to want speedy tests and the sorting method can be brutally inefficient for larger indices (like if you were using rows instead of columns for this problem). The sort method is also susceptible to false negatives on non-unique indices.
Fortunately, pandas.util.testing.assert_frame_equal
has since been updated with a check_like
option. Set this to true and the ordering will not be considered in the test.
With non-unique indices, you'll get the cryptic ValueError: cannot reindex from a duplicate axis
. This is raised by the under-the-hood reindex_like
operation that rearranges one of the DataFrames to match the other's order. Reindexing is much faster than sorting as evidenced below.
import pandas as pd
from pandas.util.testing import assert_frame_equal
df = pd.DataFrame(np.arange(1e6))
df1 = df.sample(frac=1, random_state=42)
df2 = df.sample(frac=1, random_state=43)
%timeit -n 1 -r 5 assert_frame_equal(df1.sort_index(), df2.sort_index())
## 5.73 s ± 329 ms per loop (mean ± std. dev. of 5 runs, 1 loop each)
%timeit -n 1 -r 5 assert_frame_equal(df1, df2, check_like=True)
## 1.04 s ± 237 ms per loop (mean ± std. dev. of 5 runs, 1 loop each)
For those who enjoy a good performance comparison plot:
Reindexing vs sorting on int and str indices (str even more drastic)
def equal( df1, df2 ):
""" Check if two DataFrames are equal, ignoring nans """
return df1.fillna(1).sort_index(axis=1).eq(df2.fillna(1).sort_index(axis=1)).all().all()
Probably you may need function to compare DataFrames ignoring both row and column order? Only requirement is to have some unique column to use it as index.
f1 = pd.DataFrame([
{"id": 1, "foo": "1", "bar": None},
{"id": 2, "foo": "2", "bar": 2},
{"id": 3, "foo": "3", "bar": 3},
{"id": 4, "foo": "4", "bar": 4}
])
f2 = pd.DataFrame([
{"id": 3, "foo": "3", "bar": 3},
{"id": 1, "bar": None, "foo": "1",},
{"id": 2, "foo": "2", "bar": 2},
{"id": 4, "foo": "4", "bar": 4}
])
def comparable(df, index_col='id'):
return df.fillna(value=0).set_index(index_col).to_dict('index')
comparable(f1) == comparable (f2) # returns True
have you tried using df1.equals(df2)? i think it's more reliable that df1==df2, though i'm not sure if it will resolve your issues with column order.
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.equals.html
When working with dataframes containing python objects such as tuples and lists df.eq(df2)
and df == df2
will not suffice. Even if a the same cells in each dataframes contain the same object, such as (0, 0)
, the equality comparison will result to False
. To get around this, convert all columns to strings before comparison:
df.apply(lambda x: x.astype(str)).eq(df2.apply(lambda x: x.astype(str)))
You could sort the columns using sort_index:
df1.sort_index(axis=1) == df2.sort_index(axis=1)
This will evaluate to a dataframe of all True
values.
As @osa comments this fails for NaN's and isn't particularly robust either, in practise using something similar to @quant's answer is probably recommended (Note: we want a bool rather than raise if there's an issue):
def my_equal(df1, df2):
from pandas.util.testing import assert_frame_equal
try:
assert_frame_equal(df1.sort_index(axis=1), df2.sort_index(axis=1), check_names=True)
return True
except (AssertionError, ValueError, TypeError): perhaps something else?
return False