magicsword
magicsword

Reputation: 1289

Comparing 2 columns on 2 pandas dataframes for equality

I have two dataframes pd and pd2:

pd

Name A B
t1   3 4
t5   2 2
fry  4 5
net  3 3

pd2

Name A B
t1   3 4
t5   2 2
fry  4 5
net  3 3

I want to make sure that the columns 'Name' between the two dataframes match not only the names (t1,t5,etc..) but also they need to be in the same order. I've tried chekS = (df.index == df2.index).all(axis=1).astype(str) with no luck.

Upvotes: 0

Views: 294

Answers (2)

Vaishali
Vaishali

Reputation: 38425

If Name is the column not the index as your sample dataframe suggests, you can compare the two columns

(df1['Name'] == df2['Name']).all()

It returns True in this case.

Lets say your df2 is

    Name    A   B
0   t1      3   4
1   t5      2   2
2   net     3   3
3   fry     4   5

I just flipped the rows at index 2 and 3 keeping the values same,

(df1['Name'] == df2['Name']).all()

will return False

Upvotes: 1

Acccumulation
Acccumulation

Reputation: 3591

Assuming that Name is your index, you either change your axis to 0, or use chekS = sum(df.index != df2.index). If it's not the index, then chekS = sum(df.Name != df2.Name) will work.

Upvotes: 1

Related Questions