1

Assuming that I have two dataframes:

# df1
+-----------------------+
| Name_1 |Age| Location |
+-----------------------+
| A    | 18  |    UK    |
| B    | 19  |    US    |
+-----------------------+

# df2
+-------------------------+
| Name_2 | Age | Location |
+-------------------------+
| A    | 18  |    US      |
| B    | 19  |    US      |
+-------------------------+

How can I compare all of the elements and get a dataframe with boolean values that indicate whether the corresponding values match?

The desired output would be:

# desired
+-----------------------+
| Name | Age  | Location|
+-----------------------+
| A    | True |  False  |
| B    | True |  True   |
+-----------------------+

1 Answer 1

3

If same number of rows and same columns names in both DataFrames create indices by name in both by DataFrame.set_index and then compare:

df11 = df1.set_index('name')
df22 = df2.set_index('name')
df = (df11 == df22).reset_index()

EDIT: If different only columns for index:

df11 = df1.set_index('Name_1')
df22 = df2.set_index('Name_2')
df = (df11 == df22).reset_index()
print (df)
  Name_1   Age  Location
0      A  True     False
1      B  True      True

If possible different another columns, but length of columns is still same and also length of index is necessary set same columns names in both - e.g. df22 columns by df11 columns:

print (df1)
  Name_1  Age1 Location1
0      A    18        UK
1      B    19        US

print (df2)
  Name_2  Age2 Location2
0      A    18        US
1      B    19        US

df11 = df1.set_index('Name_1')
df22 = df2.set_index('Name_2')
df22.columns = df11.columns
df = (df11 == df22).reset_index()
print (df)
  Name_1  Age1  Location1
0      A  True      False
1      B  True       True
Sign up to request clarification or add additional context in comments.

3 Comments

if you are using tabulate and if I may assume Name is already the index, you probably only need df11==df22
@eliu - then df1 == df2
Would this work even if I have different column names? See my updated question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.