Reputation: 133
I have 2 data frames df1 and df2, which have same column names but could have different order,
I am validating if these 2 dataframes are identical or not.
I want to compare these dataframes based on the column names.
df1:
A B C ...
1 1 1
1 2 4
5 3 8
df2:
A C B ....
1 1 1
1 4 2
5 8 3
I want to compare df1.A & df2.A and so on .
would really appreciate if I could get help regarding the same ,
Thank you
Upvotes: 2
Views: 944
Reputation: 1
if you don't care about the columns being in order and are just concerned with seeing if they are all contained within each dataframe:
set(df1.columns) == set(df2.columns)
Upvotes: 0
Reputation: 7316
First, sort both dataframe columns lexicographically,
df1 = df1.reindex_axis(sorted(df.columns), axis=1)
df2 = df2.reindex_axis(sorted(df.columns), axis=1)
then compare.
df1 == df2
You don't need iteration. (Which means way faster)
Upvotes: 1